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