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::{DUMMY_SP, Span, Spanned, Symbol, sym};
11use thin_vec::ThinVec;
12
13use crate::errors;
14
15macro_rules! gate {
17 ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{
18 if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
19 feature_err(&$visitor.sess, sym::$feature, $span, $explain).emit();
20 }
21 }};
22 ($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{
23 if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
24 feature_err(&$visitor.sess, sym::$feature, $span, $explain).with_help($help).emit();
25 }
26 }};
27}
28
29macro_rules! gate_alt {
31 ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr) => {{
32 if !$has_feature && !$span.allows_unstable($name) {
33 feature_err(&$visitor.sess, $name, $span, $explain).emit();
34 }
35 }};
36 ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr, $notes: expr) => {{
37 if !$has_feature && !$span.allows_unstable($name) {
38 let mut diag = feature_err(&$visitor.sess, $name, $span, $explain);
39 for note in $notes {
40 diag.note(*note);
41 }
42 diag.emit();
43 }
44 }};
45}
46
47macro_rules! gate_multi {
49 ($visitor:expr, $feature:ident, $spans:expr, $explain:expr) => {{
50 if !$visitor.features.$feature() {
51 let spans: Vec<_> =
52 $spans.filter(|span| !span.allows_unstable(sym::$feature)).collect();
53 if !spans.is_empty() {
54 feature_err(&$visitor.sess, sym::$feature, spans, $explain).emit();
55 }
56 }
57 }};
58}
59
60macro_rules! gate_legacy {
62 ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{
63 if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
64 feature_warn(&$visitor.sess, sym::$feature, $span, $explain);
65 }
66 }};
67}
68
69pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) {
70 PostExpansionVisitor { sess, features }.visit_attribute(attr)
71}
72
73struct PostExpansionVisitor<'a> {
74 sess: &'a Session,
75
76 features: &'a Features,
78}
79
80impl<'a> PostExpansionVisitor<'a> {
81 fn check_impl_trait(&self, ty: &ast::Ty, in_associated_ty: bool) {
83 struct ImplTraitVisitor<'a> {
84 vis: &'a PostExpansionVisitor<'a>,
85 in_associated_ty: bool,
86 }
87 impl Visitor<'_> for ImplTraitVisitor<'_> {
88 fn visit_ty(&mut self, ty: &ast::Ty) {
89 if let ast::TyKind::ImplTrait(..) = ty.kind {
90 if self.in_associated_ty {
91 {
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!(
92 &self.vis,
93 impl_trait_in_assoc_type,
94 ty.span,
95 "`impl Trait` in associated types is unstable"
96 );
97 } else {
98 {
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!(
99 &self.vis,
100 type_alias_impl_trait,
101 ty.span,
102 "`impl Trait` in type aliases is unstable"
103 );
104 }
105 }
106 visit::walk_ty(self, ty);
107 }
108
109 fn visit_anon_const(&mut self, _: &ast::AnonConst) -> Self::Result {
110 }
115 }
116 ImplTraitVisitor { vis: self, in_associated_ty }.visit_ty(ty);
117 }
118
119 fn check_late_bound_lifetime_defs(&self, params: &[ast::GenericParam]) {
120 let non_lt_param_spans = params.iter().filter_map(|param| match param.kind {
123 ast::GenericParamKind::Lifetime { .. } => None,
124 _ => Some(param.ident.span),
125 });
126 {
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!(
127 &self,
128 non_lifetime_binders,
129 non_lt_param_spans,
130 msg!("only lifetime parameters can be used in this context")
131 );
132
133 if self.features.non_lifetime_binders() {
136 let const_param_spans: Vec<_> = params
137 .iter()
138 .filter_map(|param| match param.kind {
139 ast::GenericParamKind::Const { .. } => Some(param.ident.span),
140 _ => None,
141 })
142 .collect();
143
144 if !const_param_spans.is_empty() {
145 self.sess.dcx().emit_err(errors::ForbiddenConstParam { const_param_spans });
146 }
147 }
148
149 for param in params {
150 if !param.bounds.is_empty() {
151 let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
152 self.sess.dcx().emit_err(errors::ForbiddenBound { spans });
153 }
154 }
155 }
156}
157
158impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
159 fn visit_attribute(&mut self, attr: &ast::Attribute) {
160 let attr_info = attr.name().and_then(|name| BUILTIN_ATTRIBUTE_MAP.get(&name));
161 if let Some(BuiltinAttribute {
163 gate: AttributeGate::Gated { feature, message, check, notes, .. },
164 ..
165 }) = attr_info
166 {
167 {
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);
168 }
169 if attr.has_name(sym::doc) {
171 for meta_item_inner in attr.meta_item_list().unwrap_or_default() {
172 macro_rules! gate_doc { ($($s:literal { $($name:ident => $feature:ident)* })*) => {
173 $($(if meta_item_inner.has_name(sym::$name) {
174 let msg = concat!("`#[doc(", stringify!($name), ")]` is ", $s);
175 gate!(self, $feature, attr.span, msg);
176 })*)*
177 }}
178
179 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!(
180 "experimental" {
181 cfg => doc_cfg
182 auto_cfg => doc_cfg
183 masked => doc_masked
184 notable_trait => doc_notable_trait
185 }
186 "meant for internal use only" {
187 attribute => rustdoc_internals
188 keyword => rustdoc_internals
189 fake_variadic => rustdoc_internals
190 search_unbox => rustdoc_internals
191 }
192 );
193 }
194 }
195 }
196
197 fn visit_item(&mut self, i: &'a ast::Item) {
198 match &i.kind {
199 ast::ItemKind::ForeignMod(_foreign_module) => {
200 }
202 ast::ItemKind::Struct(..) | ast::ItemKind::Enum(..) | ast::ItemKind::Union(..) => {
203 for attr in attr::filter_by_name(&i.attrs, sym::repr) {
204 for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
205 if item.has_name(sym::simd) {
206 {
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!(
207 &self,
208 repr_simd,
209 attr.span,
210 "SIMD types are experimental and possibly buggy"
211 );
212 }
213 }
214 }
215 }
216
217 ast::ItemKind::Impl(ast::Impl { of_trait: Some(of_trait), .. }) => {
218 if let ast::ImplPolarity::Negative(span) = of_trait.polarity {
219 {
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!(
220 &self,
221 negative_impls,
222 span.to(of_trait.trait_ref.path.span),
223 "negative trait bounds are not fully implemented; \
224 use marker types for now"
225 );
226 }
227
228 if let ast::Defaultness::Default(_) = of_trait.defaultness {
229 {
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");
230 }
231 }
232
233 ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => {
234 {
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!(
235 &self,
236 auto_traits,
237 i.span,
238 "auto traits are experimental and possibly buggy"
239 );
240 }
241
242 ast::ItemKind::TraitAlias(..) => {
243 {
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");
244 }
245
246 ast::ItemKind::MacroDef(_, ast::MacroDef { macro_rules: false, .. }) => {
247 let msg = "`macro` is experimental";
248 {
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);
249 }
250
251 ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => {
252 self.check_impl_trait(ty, false)
253 }
254 ast::ItemKind::Const(box ast::ConstItem {
255 rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
256 ..
257 }) => {
258 {
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");
261 }
262
263 _ => {}
264 }
265
266 visit::walk_item(self, i);
267 }
268
269 fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
270 match i.kind {
271 ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
272 let link_name = attr::first_attr_value_str_by_name(&i.attrs, sym::link_name);
273 let links_to_llvm = link_name.is_some_and(|val| val.as_str().starts_with("llvm."));
274 if links_to_llvm {
275 {
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!(
276 &self,
277 link_llvm_intrinsics,
278 i.span,
279 "linking to LLVM intrinsics is experimental"
280 );
281 }
282 }
283 ast::ForeignItemKind::TyAlias(..) => {
284 {
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");
285 }
286 ast::ForeignItemKind::MacCall(..) => {}
287 }
288
289 visit::walk_item(self, i)
290 }
291
292 fn visit_ty(&mut self, ty: &'a ast::Ty) {
293 match &ty.kind {
294 ast::TyKind::FnPtr(fn_ptr_ty) => {
295 self.check_late_bound_lifetime_defs(&fn_ptr_ty.generic_params);
297 }
298 ast::TyKind::Never => {
299 {
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");
300 }
301 ast::TyKind::Pat(..) => {
302 {
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");
303 }
304 _ => {}
305 }
306 visit::walk_ty(self, ty)
307 }
308
309 fn visit_where_predicate_kind(&mut self, kind: &'a ast::WherePredicateKind) {
310 if let ast::WherePredicateKind::BoundPredicate(bound) = kind {
311 self.check_late_bound_lifetime_defs(&bound.bound_generic_params);
313 }
314 visit::walk_where_predicate_kind(self, kind);
315 }
316
317 fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
318 if let ast::FnRetTy::Ty(output_ty) = ret_ty {
319 if let ast::TyKind::Never = output_ty.kind {
320 } else {
322 self.visit_ty(output_ty)
323 }
324 }
325 }
326
327 fn visit_generic_args(&mut self, args: &'a ast::GenericArgs) {
328 if let ast::GenericArgs::Parenthesized(generic_args) = args
332 && let ast::FnRetTy::Ty(ref ty) = generic_args.output
333 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::Never => true,
_ => false,
}matches!(ty.kind, ast::TyKind::Never)
334 {
335 {
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");
336 }
337 visit::walk_generic_args(self, args);
338 }
339
340 fn visit_expr(&mut self, e: &'a ast::Expr) {
341 match e.kind {
342 ast::ExprKind::TryBlock(_, None) => {
343 {
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");
345 }
346 ast::ExprKind::TryBlock(_, Some(_)) => {
347 }
349 ast::ExprKind::Lit(token::Lit {
350 kind: token::LitKind::Float | token::LitKind::Integer,
351 suffix,
352 ..
353 }) => match suffix {
354 Some(sym::f16) => {
355 {
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")
356 }
357 Some(sym::f128) => {
358 {
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")
359 }
360 _ => (),
361 },
362 _ => {}
363 }
364 visit::walk_expr(self, e)
365 }
366
367 fn visit_pat(&mut self, pattern: &'a ast::Pat) {
368 match &pattern.kind {
369 PatKind::Slice(pats) => {
370 for pat in pats {
371 let inner_pat = match &pat.kind {
372 PatKind::Ident(.., Some(pat)) => pat,
373 _ => pat,
374 };
375 if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
376 {
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!(
377 &self,
378 half_open_range_patterns_in_slices,
379 pat.span,
380 "`X..` patterns in slices are experimental"
381 );
382 }
383 }
384 }
385 PatKind::Box(..) => {
386 {
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");
387 }
388 _ => {}
389 }
390 visit::walk_pat(self, pattern)
391 }
392
393 fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
394 self.check_late_bound_lifetime_defs(&t.bound_generic_params);
395 visit::walk_poly_trait_ref(self, t);
396 }
397
398 fn visit_fn(&mut self, fn_kind: FnKind<'a>, _: &AttrVec, span: Span, _: NodeId) {
399 if let Some(_header) = fn_kind.header() {
400 }
402
403 if let FnKind::Closure(ast::ClosureBinder::For { generic_params, .. }, ..) = fn_kind {
404 self.check_late_bound_lifetime_defs(generic_params);
405 }
406
407 if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
408 {
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");
409 }
410
411 visit::walk_fn(self, fn_kind)
412 }
413
414 fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
415 let is_fn = match &i.kind {
416 ast::AssocItemKind::Fn(_) => true,
417 ast::AssocItemKind::Type(box ast::TyAlias { ty, .. }) => {
418 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
419 {
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!(
420 &self,
421 associated_type_defaults,
422 i.span,
423 "associated type defaults are unstable"
424 );
425 }
426 if let Some(ty) = ty {
427 self.check_impl_trait(ty, true);
428 }
429 false
430 }
431 ast::AssocItemKind::Const(box ast::ConstItem {
432 rhs_kind: ast::ConstItemRhsKind::TypeConst { rhs },
433 ..
434 }) => {
435 {
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!(
438 &self,
439 min_generic_const_args,
440 i.span,
441 "associated `type const` are unstable"
442 );
443 if ctxt == AssocCtxt::Trait && rhs.is_some() {
447 {
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!(
448 &self,
449 associated_type_defaults,
450 i.span,
451 "associated type defaults are unstable"
452 );
453 }
454 false
455 }
456 _ => false,
457 };
458 if let ast::Defaultness::Default(_) = i.kind.defaultness() {
459 {
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!(
461 &self,
462 self.features.specialization() || (is_fn && self.features.min_specialization()),
463 sym::specialization,
464 i.span,
465 "specialization is unstable"
466 );
467 }
468 visit::walk_assoc_item(self, i, ctxt)
469 }
470}
471
472pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
473 maybe_stage_features(sess, features, krate);
474 check_incompatible_features(sess, features);
475 check_dependent_features(sess, features);
476 check_new_solver_banned_features(sess, features);
477
478 let mut visitor = PostExpansionVisitor { sess, features };
479
480 let spans = sess.psess.gated_spans.spans.borrow();
481 macro_rules! gate_all {
482 ($gate:ident, $msg:literal) => {
483 if let Some(spans) = spans.get(&sym::$gate) {
484 for span in spans {
485 gate!(&visitor, $gate, *span, $msg);
486 }
487 }
488 };
489 ($gate:ident, $msg:literal, $help:literal) => {
490 if let Some(spans) = spans.get(&sym::$gate) {
491 for span in spans {
492 gate!(&visitor, $gate, *span, $msg, $help);
493 }
494 }
495 };
496 }
497 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!(
498 async_trait_bounds,
499 "`async` trait bounds are unstable",
500 "use the desugared name of the async trait, such as `AsyncFn`"
501 );
502 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");
503 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!(
504 closure_lifetime_binder,
505 "`for<...>` binders for closures are experimental",
506 "consider removing `for<...>`"
507 );
508 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");
509 if let Some(spans) = spans.get(&sym::yield_expr) {
511 for span in spans {
512 if (!visitor.features.coroutines() && !span.allows_unstable(sym::coroutines))
513 && (!visitor.features.gen_blocks() && !span.allows_unstable(sym::gen_blocks))
514 && (!visitor.features.yield_expr() && !span.allows_unstable(sym::yield_expr))
515 {
516 feature_err(&visitor.sess, sym::yield_expr, *span, "yield syntax is experimental")
519 .emit();
520 }
521 }
522 }
523 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");
524 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");
525 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!(
526 half_open_range_patterns_in_slices,
527 "half-open range patterns in slices are unstable"
528 );
529 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");
530 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");
531 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");
532 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");
533 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");
534 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");
535 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");
536 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");
537 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");
538 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");
539 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");
540 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");
541 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");
542 if let Some(spans) = spans.get(&sym::associated_const_equality) {
544 for span in spans {
545 if !visitor.features.min_generic_const_args()
546 && !span.allows_unstable(sym::min_generic_const_args)
547 {
548 feature_err(
549 &visitor.sess,
550 sym::min_generic_const_args,
551 *span,
552 "associated const equality is incomplete",
553 )
554 .emit();
555 }
556 }
557 }
558 if let Some(spans) = spans.get(&sym::mgca_type_const_syntax) {
561 for span in spans {
562 if visitor.features.min_generic_const_args()
563 || visitor.features.mgca_type_const_syntax()
564 || span.allows_unstable(sym::min_generic_const_args)
565 || span.allows_unstable(sym::mgca_type_const_syntax)
566 {
567 continue;
568 }
569 feature_err(
570 &visitor.sess,
571 sym::min_generic_const_args,
572 *span,
573 "`type const` syntax is experimental",
574 )
575 .emit();
576 }
577 }
578
579 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");
580 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");
581 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");
582 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");
583 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");
584 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");
585 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");
586 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");
587 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");
588 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");
589 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");
590 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");
591 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");
592 if let Some(spans) = spans.get(&sym::impl_restriction) {
for span in spans {
{
if !(&visitor).features.impl_restriction() &&
!(*span).allows_unstable(sym::impl_restriction) {
feature_err(&(&visitor).sess, sym::impl_restriction, *span,
"`impl` restrictions are experimental").emit();
}
};
}
};gate_all!(impl_restriction, "`impl` restrictions are 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("nightly")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}