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