1use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
2use rustc_ast::{self as ast, AttrVec, NodeId, PatKind, attr, token};
3use rustc_errors::msg;
4use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute, Features};
5use rustc_session::Session;
6use rustc_session::parse::{feature_err, feature_warn};
7use rustc_span::source_map::Spanned;
8use rustc_span::{Span, Symbol, sym};
9use thin_vec::ThinVec;
10
11use crate::errors;
12
13macro_rules! gate {
15 ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{
16 if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
17 feature_err(&$visitor.sess, sym::$feature, $span, $explain).emit();
18 }
19 }};
20 ($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{
21 if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
22 feature_err(&$visitor.sess, sym::$feature, $span, $explain).with_help($help).emit();
23 }
24 }};
25}
26
27macro_rules! gate_alt {
29 ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr) => {{
30 if !$has_feature && !$span.allows_unstable($name) {
31 feature_err(&$visitor.sess, $name, $span, $explain).emit();
32 }
33 }};
34 ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr, $notes: expr) => {{
35 if !$has_feature && !$span.allows_unstable($name) {
36 let mut diag = feature_err(&$visitor.sess, $name, $span, $explain);
37 for note in $notes {
38 diag.note(*note);
39 }
40 diag.emit();
41 }
42 }};
43}
44
45macro_rules! gate_multi {
47 ($visitor:expr, $feature:ident, $spans:expr, $explain:expr) => {{
48 if !$visitor.features.$feature() {
49 let spans: Vec<_> =
50 $spans.filter(|span| !span.allows_unstable(sym::$feature)).collect();
51 if !spans.is_empty() {
52 feature_err(&$visitor.sess, sym::$feature, spans, $explain).emit();
53 }
54 }
55 }};
56}
57
58macro_rules! gate_legacy {
60 ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{
61 if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
62 feature_warn(&$visitor.sess, sym::$feature, $span, $explain);
63 }
64 }};
65}
66
67pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) {
68 PostExpansionVisitor { sess, features }.visit_attribute(attr)
69}
70
71struct PostExpansionVisitor<'a> {
72 sess: &'a Session,
73
74 features: &'a Features,
76}
77
78impl<'a> PostExpansionVisitor<'a> {
79 fn check_impl_trait(&self, ty: &ast::Ty, in_associated_ty: bool) {
81 struct ImplTraitVisitor<'a> {
82 vis: &'a PostExpansionVisitor<'a>,
83 in_associated_ty: bool,
84 }
85 impl Visitor<'_> for ImplTraitVisitor<'_> {
86 fn visit_ty(&mut self, ty: &ast::Ty) {
87 if let ast::TyKind::ImplTrait(..) = ty.kind {
88 if self.in_associated_ty {
89 {
if !(&self.vis).features.impl_trait_in_assoc_type() &&
!ty.span.allows_unstable(sym::impl_trait_in_assoc_type) {
feature_err(&(&self.vis).sess, sym::impl_trait_in_assoc_type, ty.span,
"`impl Trait` in associated types is unstable").emit();
}
};gate!(
90 &self.vis,
91 impl_trait_in_assoc_type,
92 ty.span,
93 "`impl Trait` in associated types is unstable"
94 );
95 } else {
96 {
if !(&self.vis).features.type_alias_impl_trait() &&
!ty.span.allows_unstable(sym::type_alias_impl_trait) {
feature_err(&(&self.vis).sess, sym::type_alias_impl_trait, ty.span,
"`impl Trait` in type aliases is unstable").emit();
}
};gate!(
97 &self.vis,
98 type_alias_impl_trait,
99 ty.span,
100 "`impl Trait` in type aliases is unstable"
101 );
102 }
103 }
104 visit::walk_ty(self, ty);
105 }
106
107 fn visit_anon_const(&mut self, _: &ast::AnonConst) -> Self::Result {
108 }
113 }
114 ImplTraitVisitor { vis: self, in_associated_ty }.visit_ty(ty);
115 }
116
117 fn check_late_bound_lifetime_defs(&self, params: &[ast::GenericParam]) {
118 let non_lt_param_spans = params.iter().filter_map(|param| match param.kind {
121 ast::GenericParamKind::Lifetime { .. } => None,
122 _ => Some(param.ident.span),
123 });
124 {
if !(&self).features.non_lifetime_binders() {
let spans: Vec<_> =
non_lt_param_spans.filter(|span|
!span.allows_unstable(sym::non_lifetime_binders)).collect();
if !spans.is_empty() {
feature_err(&(&self).sess, sym::non_lifetime_binders, spans,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("only lifetime parameters can be used in this context"))).emit();
}
}
};gate_multi!(
125 &self,
126 non_lifetime_binders,
127 non_lt_param_spans,
128 msg!("only lifetime parameters can be used in this context")
129 );
130
131 if self.features.non_lifetime_binders() {
134 let const_param_spans: Vec<_> = params
135 .iter()
136 .filter_map(|param| match param.kind {
137 ast::GenericParamKind::Const { .. } => Some(param.ident.span),
138 _ => None,
139 })
140 .collect();
141
142 if !const_param_spans.is_empty() {
143 self.sess.dcx().emit_err(errors::ForbiddenConstParam { const_param_spans });
144 }
145 }
146
147 for param in params {
148 if !param.bounds.is_empty() {
149 let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
150 self.sess.dcx().emit_err(errors::ForbiddenBound { spans });
151 }
152 }
153 }
154}
155
156impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
157 fn visit_attribute(&mut self, attr: &ast::Attribute) {
158 let attr_info = attr.name().and_then(|name| BUILTIN_ATTRIBUTE_MAP.get(&name));
159 if let Some(BuiltinAttribute {
161 gate: AttributeGate::Gated { feature, message, check, notes, .. },
162 ..
163 }) = attr_info
164 {
165 {
if !check(self.features) && !attr.span.allows_unstable(*feature) {
let mut diag = feature_err(&self.sess, *feature, attr.span, *message);
for note in *notes { diag.note(*note); }
diag.emit();
}
};gate_alt!(self, check(self.features), *feature, attr.span, *message, *notes);
166 }
167 if attr.has_name(sym::doc) {
169 for meta_item_inner in attr.meta_item_list().unwrap_or_default() {
170 macro_rules! gate_doc { ($($s:literal { $($name:ident => $feature:ident)* })*) => {
171 $($(if meta_item_inner.has_name(sym::$name) {
172 let msg = concat!("`#[doc(", stringify!($name), ")]` is ", $s);
173 gate!(self, $feature, attr.span, msg);
174 })*)*
175 }}
176
177 if meta_item_inner.has_name(sym::search_unbox) {
let msg = "`#[doc(search_unbox)]` is meant for internal use only";
{
if !self.features.rustdoc_internals() &&
!attr.span.allows_unstable(sym::rustdoc_internals) {
feature_err(&self.sess, sym::rustdoc_internals, attr.span,
msg).emit();
}
};
};gate_doc!(
178 "experimental" {
179 cfg => doc_cfg
180 auto_cfg => doc_cfg
181 masked => doc_masked
182 notable_trait => doc_notable_trait
183 }
184 "meant for internal use only" {
185 attribute => rustdoc_internals
186 keyword => rustdoc_internals
187 fake_variadic => rustdoc_internals
188 search_unbox => rustdoc_internals
189 }
190 );
191 }
192 }
193 }
194
195 fn visit_item(&mut self, i: &'a ast::Item) {
196 match &i.kind {
197 ast::ItemKind::ForeignMod(_foreign_module) => {
198 }
200 ast::ItemKind::Struct(..) | ast::ItemKind::Enum(..) | ast::ItemKind::Union(..) => {
201 for attr in attr::filter_by_name(&i.attrs, sym::repr) {
202 for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
203 if item.has_name(sym::simd) {
204 {
if !(&self).features.repr_simd() &&
!attr.span.allows_unstable(sym::repr_simd) {
feature_err(&(&self).sess, sym::repr_simd, attr.span,
"SIMD types are experimental and possibly buggy").emit();
}
};gate!(
205 &self,
206 repr_simd,
207 attr.span,
208 "SIMD types are experimental and possibly buggy"
209 );
210 }
211 }
212 }
213 }
214
215 ast::ItemKind::Impl(ast::Impl { of_trait: Some(of_trait), .. }) => {
216 if let ast::ImplPolarity::Negative(span) = of_trait.polarity {
217 {
if !(&self).features.negative_impls() &&
!span.to(of_trait.trait_ref.path.span).allows_unstable(sym::negative_impls)
{
feature_err(&(&self).sess, sym::negative_impls,
span.to(of_trait.trait_ref.path.span),
"negative trait bounds are not fully implemented; \
use marker types for now").emit();
}
};gate!(
218 &self,
219 negative_impls,
220 span.to(of_trait.trait_ref.path.span),
221 "negative trait bounds are not fully implemented; \
222 use marker types for now"
223 );
224 }
225
226 if let ast::Defaultness::Default(_) = of_trait.defaultness {
227 {
if !(&self).features.specialization() &&
!i.span.allows_unstable(sym::specialization) {
feature_err(&(&self).sess, sym::specialization, i.span,
"specialization is unstable").emit();
}
};gate!(&self, specialization, i.span, "specialization is unstable");
228 }
229 }
230
231 ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => {
232 {
if !(&self).features.auto_traits() &&
!i.span.allows_unstable(sym::auto_traits) {
feature_err(&(&self).sess, sym::auto_traits, i.span,
"auto traits are experimental and possibly buggy").emit();
}
};gate!(
233 &self,
234 auto_traits,
235 i.span,
236 "auto traits are experimental and possibly buggy"
237 );
238 }
239
240 ast::ItemKind::TraitAlias(..) => {
241 {
if !(&self).features.trait_alias() &&
!i.span.allows_unstable(sym::trait_alias) {
feature_err(&(&self).sess, sym::trait_alias, i.span,
"trait aliases are experimental").emit();
}
};gate!(&self, trait_alias, i.span, "trait aliases are experimental");
242 }
243
244 ast::ItemKind::MacroDef(_, ast::MacroDef { macro_rules: false, .. }) => {
245 let msg = "`macro` is experimental";
246 {
if !(&self).features.decl_macro() &&
!i.span.allows_unstable(sym::decl_macro) {
feature_err(&(&self).sess, sym::decl_macro, i.span, msg).emit();
}
};gate!(&self, decl_macro, i.span, msg);
247 }
248
249 ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => {
250 self.check_impl_trait(ty, false)
251 }
252 ast::ItemKind::Const(box ast::ConstItem {
253 rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
254 ..
255 }) => {
256 {
if !(&self).features.min_generic_const_args() &&
!i.span.allows_unstable(sym::min_generic_const_args) {
feature_err(&(&self).sess, sym::min_generic_const_args, i.span,
"top-level `type const` are unstable").emit();
}
};gate!(&self, min_generic_const_args, i.span, "top-level `type const` are unstable");
259 }
260
261 _ => {}
262 }
263
264 visit::walk_item(self, i);
265 }
266
267 fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
268 match i.kind {
269 ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
270 let link_name = attr::first_attr_value_str_by_name(&i.attrs, sym::link_name);
271 let links_to_llvm = link_name.is_some_and(|val| val.as_str().starts_with("llvm."));
272 if links_to_llvm {
273 {
if !(&self).features.link_llvm_intrinsics() &&
!i.span.allows_unstable(sym::link_llvm_intrinsics) {
feature_err(&(&self).sess, sym::link_llvm_intrinsics, i.span,
"linking to LLVM intrinsics is experimental").emit();
}
};gate!(
274 &self,
275 link_llvm_intrinsics,
276 i.span,
277 "linking to LLVM intrinsics is experimental"
278 );
279 }
280 }
281 ast::ForeignItemKind::TyAlias(..) => {
282 {
if !(&self).features.extern_types() &&
!i.span.allows_unstable(sym::extern_types) {
feature_err(&(&self).sess, sym::extern_types, i.span,
"extern types are experimental").emit();
}
};gate!(&self, extern_types, i.span, "extern types are experimental");
283 }
284 ast::ForeignItemKind::MacCall(..) => {}
285 }
286
287 visit::walk_item(self, i)
288 }
289
290 fn visit_ty(&mut self, ty: &'a ast::Ty) {
291 match &ty.kind {
292 ast::TyKind::FnPtr(fn_ptr_ty) => {
293 self.check_late_bound_lifetime_defs(&fn_ptr_ty.generic_params);
295 }
296 ast::TyKind::Never => {
297 {
if !(&self).features.never_type() &&
!ty.span.allows_unstable(sym::never_type) {
feature_err(&(&self).sess, sym::never_type, ty.span,
"the `!` type is experimental").emit();
}
};gate!(&self, never_type, ty.span, "the `!` type is experimental");
298 }
299 ast::TyKind::Pat(..) => {
300 {
if !(&self).features.pattern_types() &&
!ty.span.allows_unstable(sym::pattern_types) {
feature_err(&(&self).sess, sym::pattern_types, ty.span,
"pattern types are unstable").emit();
}
};gate!(&self, pattern_types, ty.span, "pattern types are unstable");
301 }
302 _ => {}
303 }
304 visit::walk_ty(self, ty)
305 }
306
307 fn visit_where_predicate_kind(&mut self, kind: &'a ast::WherePredicateKind) {
308 if let ast::WherePredicateKind::BoundPredicate(bound) = kind {
309 self.check_late_bound_lifetime_defs(&bound.bound_generic_params);
311 }
312 visit::walk_where_predicate_kind(self, kind);
313 }
314
315 fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
316 if let ast::FnRetTy::Ty(output_ty) = ret_ty {
317 if let ast::TyKind::Never = output_ty.kind {
318 } else {
320 self.visit_ty(output_ty)
321 }
322 }
323 }
324
325 fn visit_generic_args(&mut self, args: &'a ast::GenericArgs) {
326 if let ast::GenericArgs::Parenthesized(generic_args) = args
330 && let ast::FnRetTy::Ty(ref ty) = generic_args.output
331 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::Never => true,
_ => false,
}matches!(ty.kind, ast::TyKind::Never)
332 {
333 {
if !(&self).features.never_type() &&
!ty.span.allows_unstable(sym::never_type) {
feature_err(&(&self).sess, sym::never_type, ty.span,
"the `!` type is experimental").emit();
}
};gate!(&self, never_type, ty.span, "the `!` type is experimental");
334 }
335 visit::walk_generic_args(self, args);
336 }
337
338 fn visit_expr(&mut self, e: &'a ast::Expr) {
339 match e.kind {
340 ast::ExprKind::TryBlock(_, None) => {
341 {
if !(&self).features.try_blocks() &&
!e.span.allows_unstable(sym::try_blocks) {
feature_err(&(&self).sess, sym::try_blocks, e.span,
"`try` expression is experimental").emit();
}
};gate!(&self, try_blocks, e.span, "`try` expression is experimental");
343 }
344 ast::ExprKind::TryBlock(_, Some(_)) => {
345 }
347 ast::ExprKind::Lit(token::Lit {
348 kind: token::LitKind::Float | token::LitKind::Integer,
349 suffix,
350 ..
351 }) => match suffix {
352 Some(sym::f16) => {
353 {
if !(&self).features.f16() && !e.span.allows_unstable(sym::f16) {
feature_err(&(&self).sess, sym::f16, e.span,
"the type `f16` is unstable").emit();
}
}gate!(&self, f16, e.span, "the type `f16` is unstable")
354 }
355 Some(sym::f128) => {
356 {
if !(&self).features.f128() && !e.span.allows_unstable(sym::f128) {
feature_err(&(&self).sess, sym::f128, e.span,
"the type `f128` is unstable").emit();
}
}gate!(&self, f128, e.span, "the type `f128` is unstable")
357 }
358 _ => (),
359 },
360 _ => {}
361 }
362 visit::walk_expr(self, e)
363 }
364
365 fn visit_pat(&mut self, pattern: &'a ast::Pat) {
366 match &pattern.kind {
367 PatKind::Slice(pats) => {
368 for pat in pats {
369 let inner_pat = match &pat.kind {
370 PatKind::Ident(.., Some(pat)) => pat,
371 _ => pat,
372 };
373 if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
374 {
if !(&self).features.half_open_range_patterns_in_slices() &&
!pat.span.allows_unstable(sym::half_open_range_patterns_in_slices)
{
feature_err(&(&self).sess, sym::half_open_range_patterns_in_slices,
pat.span, "`X..` patterns in slices are experimental").emit();
}
};gate!(
375 &self,
376 half_open_range_patterns_in_slices,
377 pat.span,
378 "`X..` patterns in slices are experimental"
379 );
380 }
381 }
382 }
383 PatKind::Box(..) => {
384 {
if !(&self).features.box_patterns() &&
!pattern.span.allows_unstable(sym::box_patterns) {
feature_err(&(&self).sess, sym::box_patterns, pattern.span,
"box pattern syntax is experimental").emit();
}
};gate!(&self, box_patterns, pattern.span, "box pattern syntax is experimental");
385 }
386 _ => {}
387 }
388 visit::walk_pat(self, pattern)
389 }
390
391 fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
392 self.check_late_bound_lifetime_defs(&t.bound_generic_params);
393 visit::walk_poly_trait_ref(self, t);
394 }
395
396 fn visit_fn(&mut self, fn_kind: FnKind<'a>, _: &AttrVec, span: Span, _: NodeId) {
397 if let Some(_header) = fn_kind.header() {
398 }
400
401 if let FnKind::Closure(ast::ClosureBinder::For { generic_params, .. }, ..) = fn_kind {
402 self.check_late_bound_lifetime_defs(generic_params);
403 }
404
405 if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
406 {
if !(&self).features.c_variadic() &&
!span.allows_unstable(sym::c_variadic) {
feature_err(&(&self).sess, sym::c_variadic, span,
"C-variadic functions are unstable").emit();
}
};gate!(&self, c_variadic, span, "C-variadic functions are unstable");
407 }
408
409 visit::walk_fn(self, fn_kind)
410 }
411
412 fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
413 let is_fn = match &i.kind {
414 ast::AssocItemKind::Fn(_) => true,
415 ast::AssocItemKind::Type(box ast::TyAlias { ty, .. }) => {
416 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
417 {
if !(&self).features.associated_type_defaults() &&
!i.span.allows_unstable(sym::associated_type_defaults) {
feature_err(&(&self).sess, sym::associated_type_defaults, i.span,
"associated type defaults are unstable").emit();
}
};gate!(
418 &self,
419 associated_type_defaults,
420 i.span,
421 "associated type defaults are unstable"
422 );
423 }
424 if let Some(ty) = ty {
425 self.check_impl_trait(ty, true);
426 }
427 false
428 }
429 ast::AssocItemKind::Const(box ast::ConstItem {
430 rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
431 ..
432 }) => {
433 {
if !(&self).features.min_generic_const_args() &&
!i.span.allows_unstable(sym::min_generic_const_args) {
feature_err(&(&self).sess, sym::min_generic_const_args, i.span,
"associated `type const` are unstable").emit();
}
};gate!(
436 &self,
437 min_generic_const_args,
438 i.span,
439 "associated `type const` are unstable"
440 );
441 false
442 }
443 _ => false,
444 };
445 if let ast::Defaultness::Default(_) = i.kind.defaultness() {
446 {
if !(self.features.specialization() ||
(is_fn && self.features.min_specialization())) &&
!i.span.allows_unstable(sym::specialization) {
feature_err(&(&self).sess, sym::specialization, i.span,
"specialization is unstable").emit();
}
};gate_alt!(
448 &self,
449 self.features.specialization() || (is_fn && self.features.min_specialization()),
450 sym::specialization,
451 i.span,
452 "specialization is unstable"
453 );
454 }
455 visit::walk_assoc_item(self, i, ctxt)
456 }
457}
458
459pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
460 maybe_stage_features(sess, features, krate);
461 check_incompatible_features(sess, features);
462 check_dependent_features(sess, features);
463 check_new_solver_banned_features(sess, features);
464
465 let mut visitor = PostExpansionVisitor { sess, features };
466
467 let spans = sess.psess.gated_spans.spans.borrow();
468 macro_rules! gate_all {
469 ($gate:ident, $msg:literal) => {
470 if let Some(spans) = spans.get(&sym::$gate) {
471 for span in spans {
472 gate!(&visitor, $gate, *span, $msg);
473 }
474 }
475 };
476 ($gate:ident, $msg:literal, $help:literal) => {
477 if let Some(spans) = spans.get(&sym::$gate) {
478 for span in spans {
479 gate!(&visitor, $gate, *span, $msg, $help);
480 }
481 }
482 };
483 }
484 if let Some(spans) = spans.get(&sym::async_trait_bounds) {
for span in spans {
{
if !(&visitor).features.async_trait_bounds() &&
!(*span).allows_unstable(sym::async_trait_bounds) {
feature_err(&(&visitor).sess, sym::async_trait_bounds, *span,
"`async` trait bounds are unstable").with_help("use the desugared name of the async trait, such as `AsyncFn`").emit();
}
};
}
};gate_all!(
485 async_trait_bounds,
486 "`async` trait bounds are unstable",
487 "use the desugared name of the async trait, such as `AsyncFn`"
488 );
489 if let Some(spans) = spans.get(&sym::async_for_loop) {
for span in spans {
{
if !(&visitor).features.async_for_loop() &&
!(*span).allows_unstable(sym::async_for_loop) {
feature_err(&(&visitor).sess, sym::async_for_loop, *span,
"`for await` loops are experimental").emit();
}
};
}
};gate_all!(async_for_loop, "`for await` loops are experimental");
490 if let Some(spans) = spans.get(&sym::closure_lifetime_binder) {
for span in spans {
{
if !(&visitor).features.closure_lifetime_binder() &&
!(*span).allows_unstable(sym::closure_lifetime_binder) {
feature_err(&(&visitor).sess, sym::closure_lifetime_binder,
*span,
"`for<...>` binders for closures are experimental").with_help("consider removing `for<...>`").emit();
}
};
}
};gate_all!(
491 closure_lifetime_binder,
492 "`for<...>` binders for closures are experimental",
493 "consider removing `for<...>`"
494 );
495 if let Some(spans) = spans.get(&sym::more_qualified_paths) {
for span in spans {
{
if !(&visitor).features.more_qualified_paths() &&
!(*span).allows_unstable(sym::more_qualified_paths) {
feature_err(&(&visitor).sess, sym::more_qualified_paths,
*span,
"usage of qualified paths in this context is experimental").emit();
}
};
}
};gate_all!(more_qualified_paths, "usage of qualified paths in this context is experimental");
496 if let Some(spans) = spans.get(&sym::yield_expr) {
498 for span in spans {
499 if (!visitor.features.coroutines() && !span.allows_unstable(sym::coroutines))
500 && (!visitor.features.gen_blocks() && !span.allows_unstable(sym::gen_blocks))
501 && (!visitor.features.yield_expr() && !span.allows_unstable(sym::yield_expr))
502 {
503 feature_err(&visitor.sess, sym::yield_expr, *span, "yield syntax is experimental")
506 .emit();
507 }
508 }
509 }
510 if let Some(spans) = spans.get(&sym::gen_blocks) {
for span in spans {
{
if !(&visitor).features.gen_blocks() &&
!(*span).allows_unstable(sym::gen_blocks) {
feature_err(&(&visitor).sess, sym::gen_blocks, *span,
"gen blocks are experimental").emit();
}
};
}
};gate_all!(gen_blocks, "gen blocks are experimental");
511 if let Some(spans) = spans.get(&sym::const_trait_impl) {
for span in spans {
{
if !(&visitor).features.const_trait_impl() &&
!(*span).allows_unstable(sym::const_trait_impl) {
feature_err(&(&visitor).sess, sym::const_trait_impl, *span,
"const trait impls are experimental").emit();
}
};
}
};gate_all!(const_trait_impl, "const trait impls are experimental");
512 if let Some(spans) = spans.get(&sym::half_open_range_patterns_in_slices) {
for span in spans {
{
if !(&visitor).features.half_open_range_patterns_in_slices() &&
!(*span).allows_unstable(sym::half_open_range_patterns_in_slices)
{
feature_err(&(&visitor).sess,
sym::half_open_range_patterns_in_slices, *span,
"half-open range patterns in slices are unstable").emit();
}
};
}
};gate_all!(
513 half_open_range_patterns_in_slices,
514 "half-open range patterns in slices are unstable"
515 );
516 if let Some(spans) = spans.get(&sym::try_blocks_heterogeneous) {
for span in spans {
{
if !(&visitor).features.try_blocks_heterogeneous() &&
!(*span).allows_unstable(sym::try_blocks_heterogeneous) {
feature_err(&(&visitor).sess, sym::try_blocks_heterogeneous,
*span, "`try bikeshed` expression is experimental").emit();
}
};
}
};gate_all!(try_blocks_heterogeneous, "`try bikeshed` expression is experimental");
517 if let Some(spans) = spans.get(&sym::yeet_expr) {
for span in spans {
{
if !(&visitor).features.yeet_expr() &&
!(*span).allows_unstable(sym::yeet_expr) {
feature_err(&(&visitor).sess, sym::yeet_expr, *span,
"`do yeet` expression is experimental").emit();
}
};
}
};gate_all!(yeet_expr, "`do yeet` expression is experimental");
518 if let Some(spans) = spans.get(&sym::const_closures) {
for span in spans {
{
if !(&visitor).features.const_closures() &&
!(*span).allows_unstable(sym::const_closures) {
feature_err(&(&visitor).sess, sym::const_closures, *span,
"const closures are experimental").emit();
}
};
}
};gate_all!(const_closures, "const closures are experimental");
519 if let Some(spans) = spans.get(&sym::builtin_syntax) {
for span in spans {
{
if !(&visitor).features.builtin_syntax() &&
!(*span).allows_unstable(sym::builtin_syntax) {
feature_err(&(&visitor).sess, sym::builtin_syntax, *span,
"`builtin #` syntax is unstable").emit();
}
};
}
};gate_all!(builtin_syntax, "`builtin #` syntax is unstable");
520 if let Some(spans) = spans.get(&sym::ergonomic_clones) {
for span in spans {
{
if !(&visitor).features.ergonomic_clones() &&
!(*span).allows_unstable(sym::ergonomic_clones) {
feature_err(&(&visitor).sess, sym::ergonomic_clones, *span,
"ergonomic clones are experimental").emit();
}
};
}
};gate_all!(ergonomic_clones, "ergonomic clones are experimental");
521 if let Some(spans) = spans.get(&sym::explicit_tail_calls) {
for span in spans {
{
if !(&visitor).features.explicit_tail_calls() &&
!(*span).allows_unstable(sym::explicit_tail_calls) {
feature_err(&(&visitor).sess, sym::explicit_tail_calls, *span,
"`become` expression is experimental").emit();
}
};
}
};gate_all!(explicit_tail_calls, "`become` expression is experimental");
522 if let Some(spans) = spans.get(&sym::generic_const_items) {
for span in spans {
{
if !(&visitor).features.generic_const_items() &&
!(*span).allows_unstable(sym::generic_const_items) {
feature_err(&(&visitor).sess, sym::generic_const_items, *span,
"generic const items are experimental").emit();
}
};
}
};gate_all!(generic_const_items, "generic const items are experimental");
523 if let Some(spans) = spans.get(&sym::guard_patterns) {
for span in spans {
{
if !(&visitor).features.guard_patterns() &&
!(*span).allows_unstable(sym::guard_patterns) {
feature_err(&(&visitor).sess, sym::guard_patterns, *span,
"guard patterns are experimental").with_help("consider using match arm guards").emit();
}
};
}
};gate_all!(guard_patterns, "guard patterns are experimental", "consider using match arm guards");
524 if let Some(spans) = spans.get(&sym::default_field_values) {
for span in spans {
{
if !(&visitor).features.default_field_values() &&
!(*span).allows_unstable(sym::default_field_values) {
feature_err(&(&visitor).sess, sym::default_field_values,
*span, "default values on fields are experimental").emit();
}
};
}
};gate_all!(default_field_values, "default values on fields are experimental");
525 if let Some(spans) = spans.get(&sym::fn_delegation) {
for span in spans {
{
if !(&visitor).features.fn_delegation() &&
!(*span).allows_unstable(sym::fn_delegation) {
feature_err(&(&visitor).sess, sym::fn_delegation, *span,
"functions delegation is not yet fully implemented").emit();
}
};
}
};gate_all!(fn_delegation, "functions delegation is not yet fully implemented");
526 if let Some(spans) = spans.get(&sym::postfix_match) {
for span in spans {
{
if !(&visitor).features.postfix_match() &&
!(*span).allows_unstable(sym::postfix_match) {
feature_err(&(&visitor).sess, sym::postfix_match, *span,
"postfix match is experimental").emit();
}
};
}
};gate_all!(postfix_match, "postfix match is experimental");
527 if let Some(spans) = spans.get(&sym::mut_ref) {
for span in spans {
{
if !(&visitor).features.mut_ref() &&
!(*span).allows_unstable(sym::mut_ref) {
feature_err(&(&visitor).sess, sym::mut_ref, *span,
"mutable by-reference bindings are experimental").emit();
}
};
}
};gate_all!(mut_ref, "mutable by-reference bindings are experimental");
528 if let Some(spans) = spans.get(&sym::min_generic_const_args) {
for span in spans {
{
if !(&visitor).features.min_generic_const_args() &&
!(*span).allows_unstable(sym::min_generic_const_args) {
feature_err(&(&visitor).sess, sym::min_generic_const_args,
*span,
"unbraced const blocks as const args are experimental").emit();
}
};
}
};gate_all!(min_generic_const_args, "unbraced const blocks as const args are experimental");
529 if let Some(spans) = spans.get(&sym::associated_const_equality) {
531 for span in spans {
532 if !visitor.features.min_generic_const_args()
533 && !span.allows_unstable(sym::min_generic_const_args)
534 {
535 feature_err(
536 &visitor.sess,
537 sym::min_generic_const_args,
538 *span,
539 "associated const equality is incomplete",
540 )
541 .emit();
542 }
543 }
544 }
545 if let Some(spans) = spans.get(&sym::mgca_type_const_syntax) {
548 for span in spans {
549 if visitor.features.min_generic_const_args()
550 || visitor.features.mgca_type_const_syntax()
551 || span.allows_unstable(sym::min_generic_const_args)
552 || span.allows_unstable(sym::mgca_type_const_syntax)
553 {
554 continue;
555 }
556 feature_err(
557 &visitor.sess,
558 sym::min_generic_const_args,
559 *span,
560 "`type const` syntax is experimental",
561 )
562 .emit();
563 }
564 }
565
566 if let Some(spans) = spans.get(&sym::global_registration) {
for span in spans {
{
if !(&visitor).features.global_registration() &&
!(*span).allows_unstable(sym::global_registration) {
feature_err(&(&visitor).sess, sym::global_registration, *span,
"global registration is experimental").emit();
}
};
}
};gate_all!(global_registration, "global registration is experimental");
567 if let Some(spans) = spans.get(&sym::return_type_notation) {
for span in spans {
{
if !(&visitor).features.return_type_notation() &&
!(*span).allows_unstable(sym::return_type_notation) {
feature_err(&(&visitor).sess, sym::return_type_notation,
*span, "return type notation is experimental").emit();
}
};
}
};gate_all!(return_type_notation, "return type notation is experimental");
568 if let Some(spans) = spans.get(&sym::pin_ergonomics) {
for span in spans {
{
if !(&visitor).features.pin_ergonomics() &&
!(*span).allows_unstable(sym::pin_ergonomics) {
feature_err(&(&visitor).sess, sym::pin_ergonomics, *span,
"pinned reference syntax is experimental").emit();
}
};
}
};gate_all!(pin_ergonomics, "pinned reference syntax is experimental");
569 if let Some(spans) = spans.get(&sym::unsafe_fields) {
for span in spans {
{
if !(&visitor).features.unsafe_fields() &&
!(*span).allows_unstable(sym::unsafe_fields) {
feature_err(&(&visitor).sess, sym::unsafe_fields, *span,
"`unsafe` fields are experimental").emit();
}
};
}
};gate_all!(unsafe_fields, "`unsafe` fields are experimental");
570 if let Some(spans) = spans.get(&sym::unsafe_binders) {
for span in spans {
{
if !(&visitor).features.unsafe_binders() &&
!(*span).allows_unstable(sym::unsafe_binders) {
feature_err(&(&visitor).sess, sym::unsafe_binders, *span,
"unsafe binder types are experimental").emit();
}
};
}
};gate_all!(unsafe_binders, "unsafe binder types are experimental");
571 if let Some(spans) = spans.get(&sym::contracts) {
for span in spans {
{
if !(&visitor).features.contracts() &&
!(*span).allows_unstable(sym::contracts) {
feature_err(&(&visitor).sess, sym::contracts, *span,
"contracts are incomplete").emit();
}
};
}
};gate_all!(contracts, "contracts are incomplete");
572 if let Some(spans) = spans.get(&sym::contracts_internals) {
for span in spans {
{
if !(&visitor).features.contracts_internals() &&
!(*span).allows_unstable(sym::contracts_internals) {
feature_err(&(&visitor).sess, sym::contracts_internals, *span,
"contract internal machinery is for internal use only").emit();
}
};
}
};gate_all!(contracts_internals, "contract internal machinery is for internal use only");
573 if let Some(spans) = spans.get(&sym::where_clause_attrs) {
for span in spans {
{
if !(&visitor).features.where_clause_attrs() &&
!(*span).allows_unstable(sym::where_clause_attrs) {
feature_err(&(&visitor).sess, sym::where_clause_attrs, *span,
"attributes in `where` clause are unstable").emit();
}
};
}
};gate_all!(where_clause_attrs, "attributes in `where` clause are unstable");
574 if let Some(spans) = spans.get(&sym::super_let) {
for span in spans {
{
if !(&visitor).features.super_let() &&
!(*span).allows_unstable(sym::super_let) {
feature_err(&(&visitor).sess, sym::super_let, *span,
"`super let` is experimental").emit();
}
};
}
};gate_all!(super_let, "`super let` is experimental");
575 if let Some(spans) = spans.get(&sym::frontmatter) {
for span in spans {
{
if !(&visitor).features.frontmatter() &&
!(*span).allows_unstable(sym::frontmatter) {
feature_err(&(&visitor).sess, sym::frontmatter, *span,
"frontmatters are experimental").emit();
}
};
}
};gate_all!(frontmatter, "frontmatters are experimental");
576 if let Some(spans) = spans.get(&sym::coroutines) {
for span in spans {
{
if !(&visitor).features.coroutines() &&
!(*span).allows_unstable(sym::coroutines) {
feature_err(&(&visitor).sess, sym::coroutines, *span,
"coroutine syntax is experimental").emit();
}
};
}
};gate_all!(coroutines, "coroutine syntax is experimental");
577 if let Some(spans) = spans.get(&sym::const_block_items) {
for span in spans {
{
if !(&visitor).features.const_block_items() &&
!(*span).allows_unstable(sym::const_block_items) {
feature_err(&(&visitor).sess, sym::const_block_items, *span,
"const block items are experimental").emit();
}
};
}
};gate_all!(const_block_items, "const block items are experimental");
578 if let Some(spans) = spans.get(&sym::final_associated_functions) {
for span in spans {
{
if !(&visitor).features.final_associated_functions() &&
!(*span).allows_unstable(sym::final_associated_functions) {
feature_err(&(&visitor).sess, sym::final_associated_functions,
*span, "`final` on trait functions is experimental").emit();
}
};
}
};gate_all!(final_associated_functions, "`final` on trait functions is experimental");
579
580 if !visitor.features.never_patterns() {
581 if let Some(spans) = spans.get(&sym::never_patterns) {
582 for &span in spans {
583 if span.allows_unstable(sym::never_patterns) {
584 continue;
585 }
586 let sm = sess.source_map();
587 if let Ok(snippet) = sm.span_to_snippet(span)
591 && snippet == "!"
592 {
593 feature_err(sess, sym::never_patterns, span, "`!` patterns are experimental")
594 .emit();
595 } else {
596 let suggestion = span.shrink_to_hi();
597 sess.dcx().emit_err(errors::MatchArmWithNoBody { span, suggestion });
598 }
599 }
600 }
601 }
602
603 if !visitor.features.negative_bounds() {
604 for &span in spans.get(&sym::negative_bounds).iter().copied().flatten() {
605 sess.dcx().emit_err(errors::NegativeBoundUnsupported { span });
606 }
607 }
608
609 macro_rules! gate_all_legacy_dont_use {
614 ($gate:ident, $msg:literal) => {
615 for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
616 gate_legacy!(&visitor, $gate, *span, $msg);
617 }
618 };
619 }
620
621 for span in spans.get(&sym::box_patterns).unwrap_or(&::alloc::vec::Vec::new())
{
{
if !(&visitor).features.box_patterns() &&
!(*span).allows_unstable(sym::box_patterns) {
feature_warn(&(&visitor).sess, sym::box_patterns, *span,
"box pattern syntax is experimental");
}
};
};gate_all_legacy_dont_use!(box_patterns, "box pattern syntax is experimental");
622 for span in spans.get(&sym::trait_alias).unwrap_or(&::alloc::vec::Vec::new())
{
{
if !(&visitor).features.trait_alias() &&
!(*span).allows_unstable(sym::trait_alias) {
feature_warn(&(&visitor).sess, sym::trait_alias, *span,
"trait aliases are experimental");
}
};
};gate_all_legacy_dont_use!(trait_alias, "trait aliases are experimental");
623 for span in spans.get(&sym::decl_macro).unwrap_or(&::alloc::vec::Vec::new()) {
{
if !(&visitor).features.decl_macro() &&
!(*span).allows_unstable(sym::decl_macro) {
feature_warn(&(&visitor).sess, sym::decl_macro, *span,
"`macro` is experimental");
}
};
};gate_all_legacy_dont_use!(decl_macro, "`macro` is experimental");
624 for span in spans.get(&sym::try_blocks).unwrap_or(&::alloc::vec::Vec::new()) {
{
if !(&visitor).features.try_blocks() &&
!(*span).allows_unstable(sym::try_blocks) {
feature_warn(&(&visitor).sess, sym::try_blocks, *span,
"`try` blocks are unstable");
}
};
};gate_all_legacy_dont_use!(try_blocks, "`try` blocks are unstable");
625 for span in spans.get(&sym::auto_traits).unwrap_or(&::alloc::vec::Vec::new())
{
{
if !(&visitor).features.auto_traits() &&
!(*span).allows_unstable(sym::auto_traits) {
feature_warn(&(&visitor).sess, sym::auto_traits, *span,
"`auto` traits are unstable");
}
};
};gate_all_legacy_dont_use!(auto_traits, "`auto` traits are unstable");
626
627 visit::walk_crate(&mut visitor, krate);
628}
629
630fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate) {
631 if sess.opts.unstable_features.is_nightly_build() {
633 return;
634 }
635 if features.enabled_features().is_empty() {
636 return;
637 }
638 let mut errored = false;
639 for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) {
640 let mut err = errors::FeatureOnNonNightly {
642 span: attr.span,
643 channel: ::core::option::Option::Some("nightly")option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)"),
644 stable_features: ::alloc::vec::Vec::new()vec![],
645 sugg: None,
646 };
647
648 let mut all_stable = true;
649 for ident in attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident()) {
650 let name = ident.name;
651 let stable_since = features
652 .enabled_lang_features()
653 .iter()
654 .find(|feat| feat.gate_name == name)
655 .map(|feat| feat.stable_since)
656 .flatten();
657 if let Some(since) = stable_since {
658 err.stable_features.push(errors::StableFeature { name, since });
659 } else {
660 all_stable = false;
661 }
662 }
663 if all_stable {
664 err.sugg = Some(attr.span);
665 }
666 sess.dcx().emit_err(err);
667 errored = true;
668 }
669 if !errored { ::core::panicking::panic("assertion failed: errored") };assert!(errored);
671}
672
673fn check_incompatible_features(sess: &Session, features: &Features) {
674 let enabled_features = features.enabled_features_iter_stable_order();
675
676 for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
677 .iter()
678 .filter(|(f1, f2)| features.enabled(*f1) && features.enabled(*f2))
679 {
680 if let Some((f1_name, f1_span)) = enabled_features.clone().find(|(name, _)| name == f1)
681 && let Some((f2_name, f2_span)) = enabled_features.clone().find(|(name, _)| name == f2)
682 {
683 let spans = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[f1_span, f2_span]))vec![f1_span, f2_span];
684 sess.dcx().emit_err(errors::IncompatibleFeatures { spans, f1: f1_name, f2: f2_name });
685 }
686 }
687}
688
689fn check_dependent_features(sess: &Session, features: &Features) {
690 for &(parent, children) in
691 rustc_feature::DEPENDENT_FEATURES.iter().filter(|(parent, _)| features.enabled(*parent))
692 {
693 if children.iter().any(|f| !features.enabled(*f)) {
694 let parent_span = features
695 .enabled_features_iter_stable_order()
696 .find_map(|(name, span)| (name == parent).then_some(span))
697 .unwrap();
698 let missing = children
700 .iter()
701 .filter(|f| !features.enabled(**f))
702 .map(|s| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", s.as_str()))
})format!("`{}`", s.as_str()))
703 .intersperse(String::from(", "))
704 .collect();
705 sess.dcx().emit_err(errors::MissingDependentFeatures { parent_span, parent, missing });
706 }
707 }
708}
709
710fn check_new_solver_banned_features(sess: &Session, features: &Features) {
711 if !sess.opts.unstable_opts.next_solver.globally {
712 return;
713 }
714
715 if let Some(gce_span) = features
717 .enabled_lang_features()
718 .iter()
719 .find(|feat| feat.gate_name == sym::generic_const_exprs)
720 .map(|feat| feat.attr_sp)
721 {
722 #[allow(rustc::symbol_intern_string_literal)]
723 sess.dcx().emit_err(errors::IncompatibleFeatures {
724 spans: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[gce_span]))vec![gce_span],
725 f1: Symbol::intern("-Znext-solver=globally"),
726 f2: sym::generic_const_exprs,
727 });
728 }
729}