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::{AttributeGate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute, Features};
6use rustc_hir::Attribute;
7use rustc_hir::attrs::AttributeKind;
8use rustc_session::Session;
9use rustc_session::parse::{feature_err, feature_warn};
10use rustc_span::{DUMMY_SP, Span, Spanned, Symbol, sym};
11use thin_vec::ThinVec;
12
13use crate::errors;
14
15macro_rules! gate {
17 ($visitor:expr, $feature:ident, $span:expr, $explain:expr $(, $help:expr)?) => {{
18 if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
19 feature_err($visitor.sess, sym::$feature, $span, $explain)
20 $(.with_help($help))?
21 .emit();
22 }
23 }};
24}
25
26macro_rules! gate_alt {
28 ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr $(, $notes:expr)?) => {{
29 if !$has_feature && !$span.allows_unstable($name) {
30 #[allow(unused_mut)]
31 let mut diag = feature_err($visitor.sess, $name, $span, $explain);
32 $(for ¬e in $notes { diag.note(note); })?
33 diag.emit();
34 }
35 }};
36}
37
38macro_rules! gate_multi {
40 ($visitor:expr, $feature:ident, $spans:expr, $explain:expr) => {{
41 if !$visitor.features.$feature() {
42 let spans: Vec<_> =
43 $spans.filter(|span| !span.allows_unstable(sym::$feature)).collect();
44 if !spans.is_empty() {
45 feature_err($visitor.sess, sym::$feature, spans, $explain).emit();
46 }
47 }
48 }};
49}
50
51pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) {
52 PostExpansionVisitor { sess, features }.visit_attribute(attr)
53}
54
55struct PostExpansionVisitor<'a> {
56 sess: &'a Session,
57
58 features: &'a Features,
60}
61
62impl<'a> PostExpansionVisitor<'a> {
72 fn check_impl_trait(&self, ty: &ast::Ty, in_associated_ty: bool) {
74 struct ImplTraitVisitor<'a> {
75 vis: &'a PostExpansionVisitor<'a>,
76 in_associated_ty: bool,
77 }
78 impl Visitor<'_> for ImplTraitVisitor<'_> {
79 fn visit_ty(&mut self, ty: &ast::Ty) {
80 if let ast::TyKind::ImplTrait(..) = ty.kind {
81 if self.in_associated_ty {
82 {
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!(
83 self.vis,
84 impl_trait_in_assoc_type,
85 ty.span,
86 "`impl Trait` in associated types is unstable"
87 );
88 } else {
89 {
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!(
90 self.vis,
91 type_alias_impl_trait,
92 ty.span,
93 "`impl Trait` in type aliases is unstable"
94 );
95 }
96 }
97 visit::walk_ty(self, ty);
98 }
99
100 fn visit_anon_const(&mut self, _: &ast::AnonConst) -> Self::Result {
101 }
106 }
107 ImplTraitVisitor { vis: self, in_associated_ty }.visit_ty(ty);
108 }
109
110 fn check_late_bound_lifetime_defs(&self, params: &[ast::GenericParam]) {
111 let non_lt_param_spans = params.iter().filter_map(|param| match param.kind {
114 ast::GenericParamKind::Lifetime { .. } => None,
115 _ => Some(param.ident.span),
116 });
117 {
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!(
118 &self,
119 non_lifetime_binders,
120 non_lt_param_spans,
121 msg!("only lifetime parameters can be used in this context")
122 );
123
124 if self.features.non_lifetime_binders() {
127 let const_param_spans: Vec<_> = params
128 .iter()
129 .filter_map(|param| match param.kind {
130 ast::GenericParamKind::Const { .. } => Some(param.ident.span),
131 _ => None,
132 })
133 .collect();
134
135 if !const_param_spans.is_empty() {
136 self.sess.dcx().emit_err(errors::ForbiddenConstParam { const_param_spans });
137 }
138 }
139
140 for param in params {
141 if !param.bounds.is_empty() {
142 let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
143 if param.bounds.iter().any(|bound| #[allow(non_exhaustive_omitted_patterns)] match bound {
GenericBound::Trait(_) => true,
_ => false,
}matches!(bound, GenericBound::Trait(_))) {
144 self.sess.dcx().emit_fatal(errors::ForbiddenBound { spans });
148 } else {
149 self.sess.dcx().emit_err(errors::ForbiddenBound { spans });
150 }
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) {
#[allow(unused_mut)]
let mut diag = feature_err(self.sess, *feature, attr.span, *message);
for ¬e 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 impls are experimental").with_help("use marker types for now").emit();
}
};gate!(
218 self,
219 negative_impls,
220 span.to(of_trait.trait_ref.path.span),
221 "negative impls are experimental",
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 experimental").emit();
}
};gate!(self, specialization, i.span, "specialization is experimental");
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!(self, auto_traits, i.span, "auto traits are experimental and possibly buggy");
233 }
234
235 ast::ItemKind::TraitAlias(..) => {
236 {
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");
237 }
238
239 ast::ItemKind::MacroDef(_, ast::MacroDef { macro_rules: false, .. }) => {
240 let msg = "`macro` is experimental";
241 {
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);
242 }
243
244 ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => {
245 self.check_impl_trait(ty, false)
246 }
247 ast::ItemKind::Const(box ast::ConstItem {
248 rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
249 ..
250 }) => {
251 {
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");
254 }
255
256 _ => {}
257 }
258
259 visit::walk_item(self, i);
260 }
261
262 fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
263 match i.kind {
264 ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
265 let link_name = attr::first_attr_value_str_by_name(&i.attrs, sym::link_name);
266 let links_to_llvm = link_name.is_some_and(|val| val.as_str().starts_with("llvm."));
267 if links_to_llvm {
268 {
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!(
269 self,
270 link_llvm_intrinsics,
271 i.span,
272 "linking to LLVM intrinsics is experimental"
273 );
274 }
275 }
276 ast::ForeignItemKind::TyAlias(..) => {
277 {
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");
278 }
279 ast::ForeignItemKind::MacCall(..) => {}
280 }
281
282 visit::walk_item(self, i)
283 }
284
285 fn visit_ty(&mut self, ty: &'a ast::Ty) {
286 match &ty.kind {
287 ast::TyKind::FnPtr(fn_ptr_ty) => {
288 self.check_late_bound_lifetime_defs(&fn_ptr_ty.generic_params);
290 }
291 ast::TyKind::Never => {
292 {
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");
293 }
294 ast::TyKind::Pat(..) => {
295 {
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");
296 }
297 _ => {}
298 }
299 visit::walk_ty(self, ty)
300 }
301
302 fn visit_where_predicate_kind(&mut self, kind: &'a ast::WherePredicateKind) {
303 if let ast::WherePredicateKind::BoundPredicate(bound) = kind {
304 self.check_late_bound_lifetime_defs(&bound.bound_generic_params);
306 }
307 visit::walk_where_predicate_kind(self, kind);
308 }
309
310 fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
311 if let ast::FnRetTy::Ty(output_ty) = ret_ty {
312 if let ast::TyKind::Never = output_ty.kind {
313 } else {
315 self.visit_ty(output_ty)
316 }
317 }
318 }
319
320 fn visit_generic_args(&mut self, args: &'a ast::GenericArgs) {
321 if let ast::GenericArgs::Parenthesized(generic_args) = args
325 && let ast::FnRetTy::Ty(ref ty) = generic_args.output
326 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::Never => true,
_ => false,
}matches!(ty.kind, ast::TyKind::Never)
327 {
328 {
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");
329 }
330 visit::walk_generic_args(self, args);
331 }
332
333 fn visit_expr(&mut self, e: &'a ast::Expr) {
334 match e.kind {
335 ast::ExprKind::TryBlock(_, None) => {
336 {
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");
338 }
339 ast::ExprKind::TryBlock(_, Some(_)) => {
340 }
342 ast::ExprKind::Lit(token::Lit {
343 kind: token::LitKind::Float | token::LitKind::Integer,
344 suffix,
345 ..
346 }) => match suffix {
347 Some(sym::f16) => {
348 {
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")
349 }
350 Some(sym::f128) => {
351 {
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")
352 }
353 _ => (),
354 },
355 _ => {}
356 }
357 visit::walk_expr(self, e)
358 }
359
360 fn visit_pat(&mut self, pattern: &'a ast::Pat) {
361 match &pattern.kind {
362 PatKind::Slice(pats) => {
363 for pat in pats {
364 let inner_pat = match &pat.kind {
365 PatKind::Ident(.., Some(pat)) => pat,
366 _ => pat,
367 };
368 if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
369 {
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!(
370 self,
371 half_open_range_patterns_in_slices,
372 pat.span,
373 "`X..` patterns in slices are experimental"
374 );
375 }
376 }
377 }
378 PatKind::Box(..) => {
379 {
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");
380 }
381 _ => {}
382 }
383 visit::walk_pat(self, pattern)
384 }
385
386 fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
387 self.check_late_bound_lifetime_defs(&t.bound_generic_params);
388 visit::walk_poly_trait_ref(self, t);
389 }
390
391 fn visit_fn(&mut self, fn_kind: FnKind<'a>, _: &AttrVec, span: Span, _: NodeId) {
392 if let Some(_header) = fn_kind.header() {
393 }
395
396 if let FnKind::Closure(ast::ClosureBinder::For { generic_params, .. }, ..) = fn_kind {
397 self.check_late_bound_lifetime_defs(generic_params);
398 }
399
400 if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
401 {
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");
402 }
403
404 visit::walk_fn(self, fn_kind)
405 }
406
407 fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
408 let is_fn = match &i.kind {
409 ast::AssocItemKind::Fn(_) => true,
410 ast::AssocItemKind::Type(box ast::TyAlias { ty, .. }) => {
411 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
412 {
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!(
413 self,
414 associated_type_defaults,
415 i.span,
416 "associated type defaults are unstable"
417 );
418 }
419 if let Some(ty) = ty {
420 self.check_impl_trait(ty, true);
421 }
422 false
423 }
424 ast::AssocItemKind::Const(box ast::ConstItem {
425 rhs_kind: ast::ConstItemRhsKind::TypeConst { rhs },
426 ..
427 }) => {
428 {
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");
431 if ctxt == AssocCtxt::Trait && rhs.is_some() {
435 {
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!(
436 self,
437 associated_type_defaults,
438 i.span,
439 "associated type defaults are unstable"
440 );
441 }
442 false
443 }
444 _ => false,
445 };
446 if let ast::Defaultness::Default(_) = i.kind.defaultness() {
447 {
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!(
449 &self,
450 self.features.specialization() || (is_fn && self.features.min_specialization()),
451 sym::specialization,
452 i.span,
453 "specialization is experimental"
454 );
455 }
456 visit::walk_assoc_item(self, i, ctxt)
457 }
458}
459
460pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
463 maybe_stage_features(sess, features, krate);
464 check_incompatible_features(sess, features);
465 check_dependent_features(sess, features);
466 check_new_solver_banned_features(sess, features);
467
468 let mut visitor = PostExpansionVisitor { sess, features };
469
470 let spans = sess.psess.gated_spans.spans.borrow();
475 macro_rules! gate_all {
476 ($feature:ident, $explain:literal $(, $help:literal)?) => {
477 for &span in spans.get(&sym::$feature).into_iter().flatten() {
478 gate!(visitor, $feature, span, $explain $(, $help)?);
479 }
480 };
481 }
482
483 for &span in spans.get(&sym::async_for_loop).into_iter().flatten() {
{
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");
485 for &span in spans.get(&sym::builtin_syntax).into_iter().flatten() {
{
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");
486 for &span in spans.get(&sym::const_block_items).into_iter().flatten() {
{
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");
487 for &span in spans.get(&sym::const_closures).into_iter().flatten() {
{
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");
488 for &span in spans.get(&sym::const_trait_impl).into_iter().flatten() {
{
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");
489 for &span in spans.get(&sym::contracts).into_iter().flatten() {
{
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");
490 for &span in spans.get(&sym::contracts_internals).into_iter().flatten() {
{
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");
491 for &span in spans.get(&sym::coroutines).into_iter().flatten() {
{
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");
492 for &span in spans.get(&sym::default_field_values).into_iter().flatten() {
{
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");
493 for &span in spans.get(&sym::ergonomic_clones).into_iter().flatten() {
{
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");
494 for &span in spans.get(&sym::explicit_tail_calls).into_iter().flatten() {
{
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");
495 for &span in spans.get(&sym::final_associated_functions).into_iter().flatten()
{
{
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");
496 for &span in spans.get(&sym::fn_delegation).into_iter().flatten() {
{
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");
497 for &span in spans.get(&sym::frontmatter).into_iter().flatten() {
{
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");
498 for &span in spans.get(&sym::gen_blocks).into_iter().flatten() {
{
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");
499 for &span in spans.get(&sym::generic_const_items).into_iter().flatten() {
{
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");
500 for &span in spans.get(&sym::global_registration).into_iter().flatten() {
{
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");
501 for &span in spans.get(&sym::guard_patterns).into_iter().flatten() {
{
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");
502 for &span in spans.get(&sym::impl_restriction).into_iter().flatten() {
{
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");
503 for &span in spans.get(&sym::min_generic_const_args).into_iter().flatten() {
{
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");
504 for &span in spans.get(&sym::more_qualified_paths).into_iter().flatten() {
{
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");
505 for &span in spans.get(&sym::mut_ref).into_iter().flatten() {
{
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");
506 for &span in spans.get(&sym::pin_ergonomics).into_iter().flatten() {
{
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");
507 for &span in spans.get(&sym::postfix_match).into_iter().flatten() {
{
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");
508 for &span in spans.get(&sym::return_type_notation).into_iter().flatten() {
{
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");
509 for &span in spans.get(&sym::super_let).into_iter().flatten() {
{
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");
510 for &span in spans.get(&sym::try_blocks_heterogeneous).into_iter().flatten() {
{
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");
511 for &span in spans.get(&sym::unsafe_binders).into_iter().flatten() {
{
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");
512 for &span in spans.get(&sym::unsafe_fields).into_iter().flatten() {
{
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");
513 for &span in spans.get(&sym::where_clause_attrs).into_iter().flatten() {
{
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");
514 for &span in spans.get(&sym::yeet_expr).into_iter().flatten() {
{
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");
515 for &span in spans.get(&sym::async_trait_bounds).into_iter().flatten() {
{
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!(
518 async_trait_bounds,
519 "`async` trait bounds are unstable",
520 "use the desugared name of the async trait, such as `AsyncFn`"
521 );
522 for &span in spans.get(&sym::closure_lifetime_binder).into_iter().flatten() {
{
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!(
523 closure_lifetime_binder,
524 "`for<...>` binders for closures are experimental",
525 "consider removing `for<...>`"
526 );
527 for &span in
spans.get(&sym::half_open_range_patterns_in_slices).into_iter().flatten()
{
{
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!(
528 half_open_range_patterns_in_slices,
529 "half-open range patterns in slices are unstable"
530 );
531
532 for &span in spans.get(&sym::associated_const_equality).into_iter().flatten() {
534 {
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");
535 }
536
537 for &span in spans.get(&sym::mgca_type_const_syntax).into_iter().flatten() {
540 if visitor.features.min_generic_const_args()
541 || visitor.features.mgca_type_const_syntax()
542 || span.allows_unstable(sym::min_generic_const_args)
543 || span.allows_unstable(sym::mgca_type_const_syntax)
544 {
545 continue;
546 }
547 feature_err(
548 visitor.sess,
549 sym::min_generic_const_args,
550 span,
551 "`type const` syntax is experimental",
552 )
553 .emit();
554 }
555
556 if !visitor.features.negative_bounds() {
559 for &span in spans.get(&sym::negative_bounds).into_iter().flatten() {
560 sess.dcx().emit_err(errors::NegativeBoundUnsupported { span });
561 }
562 }
563
564 if !visitor.features.never_patterns() {
565 for &span in spans.get(&sym::never_patterns).into_iter().flatten() {
566 if span.allows_unstable(sym::never_patterns) {
567 continue;
568 }
569 if let Ok("!") = sess.source_map().span_to_snippet(span).as_deref() {
573 feature_err(sess, sym::never_patterns, span, "`!` patterns are experimental")
574 .emit();
575 } else {
576 let suggestion = span.shrink_to_hi();
577 sess.dcx().emit_err(errors::MatchArmWithNoBody { span, suggestion });
578 }
579 }
580 }
581
582 for &span in spans.get(&sym::yield_expr).into_iter().flatten() {
584 if (!visitor.features.coroutines() && !span.allows_unstable(sym::coroutines))
585 && (!visitor.features.gen_blocks() && !span.allows_unstable(sym::gen_blocks))
586 && (!visitor.features.yield_expr() && !span.allows_unstable(sym::yield_expr))
587 {
588 feature_err(visitor.sess, sym::yield_expr, span, "yield syntax is experimental").emit();
591 }
592 }
593
594 macro_rules! soft_gate_all_legacy_dont_use {
604 ($feature:ident, $explain:literal) => {
605 for &span in spans.get(&sym::$feature).into_iter().flatten() {
606 if !visitor.features.$feature() && !span.allows_unstable(sym::$feature) {
607 feature_warn(&visitor.sess, sym::$feature, span, $explain);
608 }
609 }
610 };
611 }
612
613 for &span in spans.get(&sym::auto_traits).into_iter().flatten() {
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");
615 for &span in spans.get(&sym::box_patterns).into_iter().flatten() {
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");
616 for &span in spans.get(&sym::decl_macro).into_iter().flatten() {
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");
617 for &span in spans.get(&sym::negative_impls).into_iter().flatten() {
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");
618 for &span in spans.get(&sym::specialization).into_iter().flatten() {
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");
619 for &span in spans.get(&sym::trait_alias).into_iter().flatten() {
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");
620 for &span in spans.get(&sym::try_blocks).into_iter().flatten() {
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");
621 for &span in spans.get(&sym::min_specialization).into_iter().flatten() {
624 if !visitor.features.specialization()
625 && !visitor.features.min_specialization()
626 && !span.allows_unstable(sym::specialization)
627 && !span.allows_unstable(sym::min_specialization)
628 {
629 feature_warn(visitor.sess, sym::specialization, span, "specialization is experimental");
630 }
631 }
632
633 visit::walk_crate(&mut visitor, krate);
636}
637
638fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate) {
639 if sess.opts.unstable_features.is_nightly_build() {
641 return;
642 }
643 if features.enabled_features().is_empty() {
644 return;
645 }
646 let mut errored = false;
647
648 if let Some(Attribute::Parsed(AttributeKind::Feature(feature_idents, first_span))) =
649 AttributeParser::parse_limited(
650 sess,
651 &krate.attrs,
652 sym::feature,
653 DUMMY_SP,
654 krate.id,
655 Some(&features),
656 )
657 {
658 let mut err = errors::FeatureOnNonNightly {
660 span: first_span,
661 channel: ::core::option::Option::Some("nightly")option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)"),
662 stable_features: ::alloc::vec::Vec::new()vec![],
663 sugg: None,
664 };
665
666 let mut all_stable = true;
667 for ident in feature_idents {
668 let name = ident.name;
669 let stable_since = features
670 .enabled_lang_features()
671 .iter()
672 .find(|feat| feat.gate_name == name)
673 .map(|feat| feat.stable_since)
674 .flatten();
675 if let Some(since) = stable_since {
676 err.stable_features.push(errors::StableFeature { name, since });
677 } else {
678 all_stable = false;
679 }
680 }
681 if all_stable {
682 err.sugg = Some(first_span);
683 }
684 sess.dcx().emit_err(err);
685 errored = true;
686 }
687 if !errored { ::core::panicking::panic("assertion failed: errored") };assert!(errored);
689}
690
691fn check_incompatible_features(sess: &Session, features: &Features) {
692 let enabled_features = features.enabled_features_iter_stable_order();
693
694 for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
695 .iter()
696 .filter(|(f1, f2)| features.enabled(*f1) && features.enabled(*f2))
697 {
698 if let Some((f1_name, f1_span)) = enabled_features.clone().find(|(name, _)| name == f1)
699 && let Some((f2_name, f2_span)) = enabled_features.clone().find(|(name, _)| name == f2)
700 {
701 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];
702 sess.dcx().emit_err(errors::IncompatibleFeatures { spans, f1: f1_name, f2: f2_name });
703 }
704 }
705}
706
707fn check_dependent_features(sess: &Session, features: &Features) {
708 for &(parent, children) in
709 rustc_feature::DEPENDENT_FEATURES.iter().filter(|(parent, _)| features.enabled(*parent))
710 {
711 if children.iter().any(|f| !features.enabled(*f)) {
712 let parent_span = features
713 .enabled_features_iter_stable_order()
714 .find_map(|(name, span)| (name == parent).then_some(span))
715 .unwrap();
716 let missing = children
718 .iter()
719 .filter(|f| !features.enabled(**f))
720 .map(|s| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", s.as_str()))
})format!("`{}`", s.as_str()))
721 .intersperse(String::from(", "))
722 .collect();
723 sess.dcx().emit_err(errors::MissingDependentFeatures { parent_span, parent, missing });
724 }
725 }
726}
727
728fn check_new_solver_banned_features(sess: &Session, features: &Features) {
729 if !sess.opts.unstable_opts.next_solver.globally {
730 return;
731 }
732
733 if let Some(gce_span) = features
735 .enabled_lang_features()
736 .iter()
737 .find(|feat| feat.gate_name == sym::generic_const_exprs)
738 .map(|feat| feat.attr_sp)
739 {
740 #[allow(rustc::symbol_intern_string_literal)]
741 sess.dcx().emit_err(errors::IncompatibleFeatures {
742 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],
743 f1: Symbol::intern("-Znext-solver=globally"),
744 f2: sym::generic_const_exprs,
745 });
746 }
747}