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