1use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
2use rustc_ast::{self as ast, AttrVec, NodeId, PatKind, attr, token};
3use rustc_errors::inline_fluent;
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 inline_fluent!("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
253 _ => {}
254 }
255
256 visit::walk_item(self, i);
257 }
258
259 fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
260 match i.kind {
261 ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
262 let link_name = attr::first_attr_value_str_by_name(&i.attrs, sym::link_name);
263 let links_to_llvm = link_name.is_some_and(|val| val.as_str().starts_with("llvm."));
264 if links_to_llvm {
265 {
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!(
266 &self,
267 link_llvm_intrinsics,
268 i.span,
269 "linking to LLVM intrinsics is experimental"
270 );
271 }
272 }
273 ast::ForeignItemKind::TyAlias(..) => {
274 {
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");
275 }
276 ast::ForeignItemKind::MacCall(..) => {}
277 }
278
279 visit::walk_item(self, i)
280 }
281
282 fn visit_ty(&mut self, ty: &'a ast::Ty) {
283 match &ty.kind {
284 ast::TyKind::FnPtr(fn_ptr_ty) => {
285 self.check_late_bound_lifetime_defs(&fn_ptr_ty.generic_params);
287 }
288 ast::TyKind::Never => {
289 {
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");
290 }
291 ast::TyKind::Pat(..) => {
292 {
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");
293 }
294 _ => {}
295 }
296 visit::walk_ty(self, ty)
297 }
298
299 fn visit_where_predicate_kind(&mut self, kind: &'a ast::WherePredicateKind) {
300 if let ast::WherePredicateKind::BoundPredicate(bound) = kind {
301 self.check_late_bound_lifetime_defs(&bound.bound_generic_params);
303 }
304 visit::walk_where_predicate_kind(self, kind);
305 }
306
307 fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
308 if let ast::FnRetTy::Ty(output_ty) = ret_ty {
309 if let ast::TyKind::Never = output_ty.kind {
310 } else {
312 self.visit_ty(output_ty)
313 }
314 }
315 }
316
317 fn visit_generic_args(&mut self, args: &'a ast::GenericArgs) {
318 if let ast::GenericArgs::Parenthesized(generic_args) = args
322 && let ast::FnRetTy::Ty(ref ty) = generic_args.output
323 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::Never => true,
_ => false,
}matches!(ty.kind, ast::TyKind::Never)
324 {
325 {
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");
326 }
327 visit::walk_generic_args(self, args);
328 }
329
330 fn visit_expr(&mut self, e: &'a ast::Expr) {
331 match e.kind {
332 ast::ExprKind::TryBlock(_, None) => {
333 {
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");
334 }
335 ast::ExprKind::TryBlock(_, Some(_)) => {
336 {
if !(&self).features.try_blocks_heterogeneous() &&
!e.span.allows_unstable(sym::try_blocks_heterogeneous) {
feature_err(&(&self).sess, sym::try_blocks_heterogeneous, e.span,
"`try bikeshed` expression is experimental").emit();
}
};gate!(
337 &self,
338 try_blocks_heterogeneous,
339 e.span,
340 "`try bikeshed` expression is experimental"
341 );
342 }
343 ast::ExprKind::Lit(token::Lit {
344 kind: token::LitKind::Float | token::LitKind::Integer,
345 suffix,
346 ..
347 }) => match suffix {
348 Some(sym::f16) => {
349 {
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")
350 }
351 Some(sym::f128) => {
352 {
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")
353 }
354 _ => (),
355 },
356 _ => {}
357 }
358 visit::walk_expr(self, e)
359 }
360
361 fn visit_pat(&mut self, pattern: &'a ast::Pat) {
362 match &pattern.kind {
363 PatKind::Slice(pats) => {
364 for pat in pats {
365 let inner_pat = match &pat.kind {
366 PatKind::Ident(.., Some(pat)) => pat,
367 _ => pat,
368 };
369 if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
370 {
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!(
371 &self,
372 half_open_range_patterns_in_slices,
373 pat.span,
374 "`X..` patterns in slices are experimental"
375 );
376 }
377 }
378 }
379 PatKind::Box(..) => {
380 {
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");
381 }
382 _ => {}
383 }
384 visit::walk_pat(self, pattern)
385 }
386
387 fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
388 self.check_late_bound_lifetime_defs(&t.bound_generic_params);
389 visit::walk_poly_trait_ref(self, t);
390 }
391
392 fn visit_fn(&mut self, fn_kind: FnKind<'a>, _: &AttrVec, span: Span, _: NodeId) {
393 if let Some(_header) = fn_kind.header() {
394 }
396
397 if let FnKind::Closure(ast::ClosureBinder::For { generic_params, .. }, ..) = fn_kind {
398 self.check_late_bound_lifetime_defs(generic_params);
399 }
400
401 if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
402 {
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");
403 }
404
405 visit::walk_fn(self, fn_kind)
406 }
407
408 fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
409 let is_fn = match &i.kind {
410 ast::AssocItemKind::Fn(_) => true,
411 ast::AssocItemKind::Type(box ast::TyAlias { ty, .. }) => {
412 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
413 {
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!(
414 &self,
415 associated_type_defaults,
416 i.span,
417 "associated type defaults are unstable"
418 );
419 }
420 if let Some(ty) = ty {
421 self.check_impl_trait(ty, true);
422 }
423 false
424 }
425 _ => false,
426 };
427 if let ast::Defaultness::Default(_) = i.kind.defaultness() {
428 {
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!(
430 &self,
431 self.features.specialization() || (is_fn && self.features.min_specialization()),
432 sym::specialization,
433 i.span,
434 "specialization is unstable"
435 );
436 }
437 visit::walk_assoc_item(self, i, ctxt)
438 }
439}
440
441pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
442 maybe_stage_features(sess, features, krate);
443 check_incompatible_features(sess, features);
444 check_new_solver_banned_features(sess, features);
445
446 let mut visitor = PostExpansionVisitor { sess, features };
447
448 let spans = sess.psess.gated_spans.spans.borrow();
449 macro_rules! gate_all {
450 ($gate:ident, $msg:literal) => {
451 if let Some(spans) = spans.get(&sym::$gate) {
452 for span in spans {
453 gate!(&visitor, $gate, *span, $msg);
454 }
455 }
456 };
457 ($gate:ident, $msg:literal, $help:literal) => {
458 if let Some(spans) = spans.get(&sym::$gate) {
459 for span in spans {
460 gate!(&visitor, $gate, *span, $msg, $help);
461 }
462 }
463 };
464 }
465 if let Some(spans) = spans.get(&sym::if_let_guard) {
for span in spans {
{
if !(&visitor).features.if_let_guard() &&
!(*span).allows_unstable(sym::if_let_guard) {
feature_err(&(&visitor).sess, sym::if_let_guard, *span,
"`if let` guards are experimental").with_help("you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`").emit();
}
};
}
};gate_all!(
466 if_let_guard,
467 "`if let` guards are experimental",
468 "you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`"
469 );
470 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!(
471 async_trait_bounds,
472 "`async` trait bounds are unstable",
473 "use the desugared name of the async trait, such as `AsyncFn`"
474 );
475 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");
476 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!(
477 closure_lifetime_binder,
478 "`for<...>` binders for closures are experimental",
479 "consider removing `for<...>`"
480 );
481 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");
482 if let Some(spans) = spans.get(&sym::yield_expr) {
484 for span in spans {
485 if (!visitor.features.coroutines() && !span.allows_unstable(sym::coroutines))
486 && (!visitor.features.gen_blocks() && !span.allows_unstable(sym::gen_blocks))
487 && (!visitor.features.yield_expr() && !span.allows_unstable(sym::yield_expr))
488 {
489 feature_err(&visitor.sess, sym::yield_expr, *span, "yield syntax is experimental")
492 .emit();
493 }
494 }
495 }
496 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");
497 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");
498 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!(
499 half_open_range_patterns_in_slices,
500 "half-open range patterns in slices are unstable"
501 );
502 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");
503 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");
504 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");
505 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");
506 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");
507 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");
508 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");
509 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");
510 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");
511 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");
512 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");
513 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");
514 if let Some(spans) = spans.get(&sym::associated_const_equality) {
516 for span in spans {
517 if !visitor.features.min_generic_const_args()
518 && !span.allows_unstable(sym::min_generic_const_args)
519 {
520 feature_err(
521 &visitor.sess,
522 sym::min_generic_const_args,
523 *span,
524 "associated const equality is incomplete",
525 )
526 .emit();
527 }
528 }
529 }
530 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");
531 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");
532 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");
533 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");
534 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");
535 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");
536 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");
537 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");
538 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");
539 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");
540 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");
541 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");
542
543 if !visitor.features.never_patterns() {
544 if let Some(spans) = spans.get(&sym::never_patterns) {
545 for &span in spans {
546 if span.allows_unstable(sym::never_patterns) {
547 continue;
548 }
549 let sm = sess.source_map();
550 if let Ok(snippet) = sm.span_to_snippet(span)
554 && snippet == "!"
555 {
556 feature_err(sess, sym::never_patterns, span, "`!` patterns are experimental")
557 .emit();
558 } else {
559 let suggestion = span.shrink_to_hi();
560 sess.dcx().emit_err(errors::MatchArmWithNoBody { span, suggestion });
561 }
562 }
563 }
564 }
565
566 if !visitor.features.negative_bounds() {
567 for &span in spans.get(&sym::negative_bounds).iter().copied().flatten() {
568 sess.dcx().emit_err(errors::NegativeBoundUnsupported { span });
569 }
570 }
571
572 macro_rules! gate_all_legacy_dont_use {
577 ($gate:ident, $msg:literal) => {
578 for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
579 gate_legacy!(&visitor, $gate, *span, $msg);
580 }
581 };
582 }
583
584 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");
585 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");
586 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");
587 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");
588 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");
589
590 visit::walk_crate(&mut visitor, krate);
591}
592
593fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate) {
594 if sess.opts.unstable_features.is_nightly_build() {
596 return;
597 }
598 if features.enabled_features().is_empty() {
599 return;
600 }
601 let mut errored = false;
602 for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) {
603 let mut err = errors::FeatureOnNonNightly {
605 span: attr.span,
606 channel: ::core::option::Option::Some("nightly")option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)"),
607 stable_features: ::alloc::vec::Vec::new()vec![],
608 sugg: None,
609 };
610
611 let mut all_stable = true;
612 for ident in attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident()) {
613 let name = ident.name;
614 let stable_since = features
615 .enabled_lang_features()
616 .iter()
617 .find(|feat| feat.gate_name == name)
618 .map(|feat| feat.stable_since)
619 .flatten();
620 if let Some(since) = stable_since {
621 err.stable_features.push(errors::StableFeature { name, since });
622 } else {
623 all_stable = false;
624 }
625 }
626 if all_stable {
627 err.sugg = Some(attr.span);
628 }
629 sess.dcx().emit_err(err);
630 errored = true;
631 }
632 if !errored { ::core::panicking::panic("assertion failed: errored") };assert!(errored);
634}
635
636fn check_incompatible_features(sess: &Session, features: &Features) {
637 let enabled_features = features.enabled_features_iter_stable_order();
638
639 for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
640 .iter()
641 .filter(|(f1, f2)| features.enabled(*f1) && features.enabled(*f2))
642 {
643 if let Some((f1_name, f1_span)) = enabled_features.clone().find(|(name, _)| name == f1)
644 && let Some((f2_name, f2_span)) = enabled_features.clone().find(|(name, _)| name == f2)
645 {
646 let spans = <[_]>::into_vec(::alloc::boxed::box_new([f1_span, f2_span]))vec![f1_span, f2_span];
647 sess.dcx().emit_err(errors::IncompatibleFeatures { spans, f1: f1_name, f2: f2_name });
648 }
649 }
650}
651
652fn check_new_solver_banned_features(sess: &Session, features: &Features) {
653 if !sess.opts.unstable_opts.next_solver.globally {
654 return;
655 }
656
657 if let Some(gce_span) = features
659 .enabled_lang_features()
660 .iter()
661 .find(|feat| feat.gate_name == sym::generic_const_exprs)
662 .map(|feat| feat.attr_sp)
663 {
664 #[allow(rustc::symbol_intern_string_literal)]
665 sess.dcx().emit_err(errors::IncompatibleFeatures {
666 spans: <[_]>::into_vec(::alloc::boxed::box_new([gce_span]))vec![gce_span],
667 f1: Symbol::intern("-Znext-solver=globally"),
668 f2: sym::generic_const_exprs,
669 });
670 }
671}