From bb3e09f1444ee10c5bbbaf971eb1e6e8994cb046 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 26 Oct 2023 13:38:38 +1100 Subject: [PATCH 1/7] Streamline `gate_feature_*` macros. The debug probably isn't useful, and assigning all the `$foo` metavariables to `foo` variables is verbose and weird. Also, `$x:expr` usually doesn't have a space after the `:`. --- Cargo.lock | 1 - compiler/rustc_ast_passes/Cargo.toml | 1 - compiler/rustc_ast_passes/src/feature_gate.rs | 44 ++++++------------- 3 files changed, 14 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b1e918e2f5ea..ba72a33e9ef00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3501,7 +3501,6 @@ dependencies = [ "rustc_span", "rustc_target", "thin-vec", - "tracing", ] [[package]] diff --git a/compiler/rustc_ast_passes/Cargo.toml b/compiler/rustc_ast_passes/Cargo.toml index c1ebfb3e6d030..0001394c8d386 100644 --- a/compiler/rustc_ast_passes/Cargo.toml +++ b/compiler/rustc_ast_passes/Cargo.toml @@ -19,5 +19,4 @@ rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } rustc_target = { path = "../rustc_target" } thin-vec = "0.2.12" -tracing = "0.1" # tidy-alphabetical-end diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index a1bd2679137d6..6288e9f8b3654 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -10,51 +10,35 @@ use rustc_span::symbol::sym; use rustc_span::Span; use rustc_target::spec::abi; use thin_vec::ThinVec; -use tracing::debug; use crate::errors; macro_rules! gate_feature_fn { - ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $help: expr) => {{ - let (visitor, has_feature, span, name, explain, help) = - (&*$visitor, $has_feature, $span, $name, $explain, $help); - let has_feature: bool = has_feature(visitor.features); - debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature); - if !has_feature && !span.allows_unstable($name) { - feature_err(&visitor.sess.parse_sess, name, span, explain).help(help).emit(); + ($visitor:expr, $has_feature:expr, $span:expr, $name:expr, $explain:expr, $help:expr) => {{ + if !$has_feature($visitor.features) && !$span.allows_unstable($name) { + feature_err(&$visitor.sess.parse_sess, $name, $span, $explain).help($help).emit(); } }}; - ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{ - let (visitor, has_feature, span, name, explain) = - (&*$visitor, $has_feature, $span, $name, $explain); - let has_feature: bool = has_feature(visitor.features); - debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature); - if !has_feature && !span.allows_unstable($name) { - feature_err(&visitor.sess.parse_sess, name, span, explain).emit(); + ($visitor:expr, $has_feature:expr, $span:expr, $name:expr, $explain:expr) => {{ + if !$has_feature($visitor.features) && !$span.allows_unstable($name) { + feature_err(&$visitor.sess.parse_sess, $name, $span, $explain).emit(); } }}; - (future_incompatible; $visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{ - let (visitor, has_feature, span, name, explain) = - (&*$visitor, $has_feature, $span, $name, $explain); - let has_feature: bool = has_feature(visitor.features); - debug!( - "gate_feature(feature = {:?}, span = {:?}); has? {} (future_incompatible)", - name, span, has_feature - ); - if !has_feature && !span.allows_unstable($name) { - feature_warn(&visitor.sess.parse_sess, name, span, explain); + (future_incompatible; $visitor:expr, $has_feature:expr, $span:expr, $name:expr, $explain:expr) => {{ + if !$has_feature($visitor.features) && !$span.allows_unstable($name) { + feature_warn(&$visitor.sess.parse_sess, $name, $span, $explain); } }}; } macro_rules! gate_feature_post { - ($visitor: expr, $feature: ident, $span: expr, $explain: expr, $help: expr) => { - gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain, $help) + ($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => { + gate_feature_fn!($visitor, |x:&Features| x.$feature, $span, sym::$feature, $explain, $help) }; - ($visitor: expr, $feature: ident, $span: expr, $explain: expr) => { - gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain) + ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => { + gate_feature_fn!($visitor, |x:&Features| x.$feature, $span, sym::$feature, $explain) }; - (future_incompatible; $visitor: expr, $feature: ident, $span: expr, $explain: expr) => { + (future_incompatible; $visitor:expr, $feature:ident, $span:expr, $explain:expr) => { gate_feature_fn!(future_incompatible; $visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain) }; } From 531b38ac2329b8078ca925d9373a5734abea0bf1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 26 Oct 2023 14:34:14 +1100 Subject: [PATCH 2/7] Cover two more cases in the `gate_doc` macro. --- compiler/rustc_ast_passes/src/feature_gate.rs | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 6288e9f8b3654..5ccdedc76ca6c 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -177,29 +177,25 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { // Check unstable flavors of the `#[doc]` attribute. if attr.has_name(sym::doc) { for nested_meta in attr.meta_item_list().unwrap_or_default() { - macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => { - $(if nested_meta.has_name(sym::$name) { - let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental"); + macro_rules! gate_doc { ($($s:literal { $($name:ident => $feature:ident)* })*) => { + $($(if nested_meta.has_name(sym::$name) { + let msg = concat!("`#[doc(", stringify!($name), ")]` is ", $s); gate_feature_post!(self, $feature, attr.span, msg); - })* + })*)* }} gate_doc!( - cfg => doc_cfg - cfg_hide => doc_cfg_hide - masked => doc_masked - notable_trait => doc_notable_trait + "experimental" { + cfg => doc_cfg + cfg_hide => doc_cfg_hide + masked => doc_masked + notable_trait => doc_notable_trait + } + "meant for internal use only" { + keyword => rustdoc_internals + fake_variadic => rustdoc_internals + } ); - - if nested_meta.has_name(sym::keyword) { - let msg = "`#[doc(keyword)]` is meant for internal use only"; - gate_feature_post!(self, rustdoc_internals, attr.span, msg); - } - - if nested_meta.has_name(sym::fake_variadic) { - let msg = "`#[doc(fake_variadic)]` is meant for internal use only"; - gate_feature_post!(self, rustdoc_internals, attr.span, msg); - } } } if !attr.is_doc_comment() From dcb72e705fccc17ca4f865708e6ba69dc680c71f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 26 Oct 2023 14:51:26 +1100 Subject: [PATCH 3/7] Use a slice pattern to neaten a condition. --- compiler/rustc_ast_passes/src/feature_gate.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 5ccdedc76ca6c..12f3d8b9e7544 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -199,17 +199,12 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } if !attr.is_doc_comment() - && attr.get_normal_item().path.segments.len() == 2 - && attr.get_normal_item().path.segments[0].ident.name == sym::diagnostic + && let [seg, _] = attr.get_normal_item().path.segments.as_slice() + && seg.ident.name == sym::diagnostic && !self.features.diagnostic_namespace { let msg = "`#[diagnostic]` attribute name space is experimental"; - gate_feature_post!( - self, - diagnostic_namespace, - attr.get_normal_item().path.segments[0].ident.span, - msg - ); + gate_feature_post!(self, diagnostic_namespace, seg.ident.span, msg); } // Emit errors for non-staged-api crates. From c88954e9c1293c84cc3f79c9293f704dd9d67b71 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 26 Oct 2023 16:49:03 +1100 Subject: [PATCH 4/7] Use `if let` to reduce some excessive indentation. --- .../rustc_ast_passes/src/ast_validation.rs | 98 +++++++++---------- 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 477d3732d464f..3d0513c89230d 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1442,62 +1442,54 @@ fn deny_equality_constraints( let mut err = errors::EqualityInWhere { span: predicate.span, assoc: None, assoc2: None }; // Given `::Bar = RhsTy`, suggest `A: Foo`. - if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind { - if let TyKind::Path(None, path) = &qself.ty.kind { - match &path.segments[..] { - [PathSegment { ident, args: None, .. }] => { - for param in &generics.params { - if param.ident == *ident { - let param = ident; - match &full_path.segments[qself.position..] { - [PathSegment { ident, args, .. }] => { - // Make a new `Path` from `foo::Bar` to `Foo`. - let mut assoc_path = full_path.clone(); - // Remove `Bar` from `Foo::Bar`. - assoc_path.segments.pop(); - let len = assoc_path.segments.len() - 1; - let gen_args = args.as_deref().cloned(); - // Build ``. - let arg = AngleBracketedArg::Constraint(AssocConstraint { - id: rustc_ast::node_id::DUMMY_NODE_ID, - ident: *ident, - gen_args, - kind: AssocConstraintKind::Equality { - term: predicate.rhs_ty.clone().into(), - }, - span: ident.span, - }); - // Add `` to `Foo`. - match &mut assoc_path.segments[len].args { - Some(args) => match args.deref_mut() { - GenericArgs::Parenthesized(_) => continue, - GenericArgs::AngleBracketed(args) => { - args.args.push(arg); - } - }, - empty_args => { - *empty_args = Some( - AngleBracketedArgs { - span: ident.span, - args: thin_vec![arg], - } - .into(), - ); - } - } - err.assoc = Some(errors::AssociatedSuggestion { - span: predicate.span, - ident: *ident, - param: *param, - path: pprust::path_to_string(&assoc_path), - }) - } - _ => {} - }; + if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind + && let TyKind::Path(None, path) = &qself.ty.kind + && let [PathSegment { ident, args: None, .. }] = &path.segments[..] + { + for param in &generics.params { + if param.ident == *ident + && let [PathSegment { ident, args, .. }] = &full_path.segments[qself.position..] + { + // Make a new `Path` from `foo::Bar` to `Foo`. + let mut assoc_path = full_path.clone(); + // Remove `Bar` from `Foo::Bar`. + assoc_path.segments.pop(); + let len = assoc_path.segments.len() - 1; + let gen_args = args.as_deref().cloned(); + // Build ``. + let arg = AngleBracketedArg::Constraint(AssocConstraint { + id: rustc_ast::node_id::DUMMY_NODE_ID, + ident: *ident, + gen_args, + kind: AssocConstraintKind::Equality { + term: predicate.rhs_ty.clone().into(), + }, + span: ident.span, + }); + // Add `` to `Foo`. + match &mut assoc_path.segments[len].args { + Some(args) => match args.deref_mut() { + GenericArgs::Parenthesized(_) => continue, + GenericArgs::AngleBracketed(args) => { + args.args.push(arg); } + }, + empty_args => { + *empty_args = Some( + AngleBracketedArgs { + span: ident.span, + args: thin_vec![arg], + } + .into(), + ); } } - _ => {} + err.assoc = Some(errors::AssociatedSuggestion { + span: predicate.span, + ident: *ident, + param: param.ident, + path: pprust::path_to_string(&assoc_path), + }) } } } From de17ec9dae991891834febb84e658373d5748548 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 30 Oct 2023 10:21:43 +1100 Subject: [PATCH 5/7] Rearrange the `gate_feature_*` macros. --- compiler/rustc_ast_passes/src/feature_gate.rs | 118 +++++++++--------- 1 file changed, 58 insertions(+), 60 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 12f3d8b9e7544..7dd6bfa13712b 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -13,34 +13,38 @@ use thin_vec::ThinVec; use crate::errors; -macro_rules! gate_feature_fn { - ($visitor:expr, $has_feature:expr, $span:expr, $name:expr, $explain:expr, $help:expr) => {{ - if !$has_feature($visitor.features) && !$span.allows_unstable($name) { - feature_err(&$visitor.sess.parse_sess, $name, $span, $explain).help($help).emit(); +/// The common case. +macro_rules! gate { + ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{ + if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) { + feature_err(&$visitor.sess.parse_sess, sym::$feature, $span, $explain).emit(); } }}; - ($visitor:expr, $has_feature:expr, $span:expr, $name:expr, $explain:expr) => {{ - if !$has_feature($visitor.features) && !$span.allows_unstable($name) { - feature_err(&$visitor.sess.parse_sess, $name, $span, $explain).emit(); + ($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{ + if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) { + feature_err(&$visitor.sess.parse_sess, sym::$feature, $span, $explain) + .help($help) + .emit(); } }}; - (future_incompatible; $visitor:expr, $has_feature:expr, $span:expr, $name:expr, $explain:expr) => {{ - if !$has_feature($visitor.features) && !$span.allows_unstable($name) { - feature_warn(&$visitor.sess.parse_sess, $name, $span, $explain); +} + +/// The unusual case, where the `has_feature` condition is non-standard. +macro_rules! gate_alt { + ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr) => {{ + if !$has_feature && !$span.allows_unstable($name) { + feature_err(&$visitor.sess.parse_sess, $name, $span, $explain).emit(); } }}; } -macro_rules! gate_feature_post { - ($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => { - gate_feature_fn!($visitor, |x:&Features| x.$feature, $span, sym::$feature, $explain, $help) - }; - ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => { - gate_feature_fn!($visitor, |x:&Features| x.$feature, $span, sym::$feature, $explain) - }; - (future_incompatible; $visitor:expr, $feature:ident, $span:expr, $explain:expr) => { - gate_feature_fn!(future_incompatible; $visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain) - }; +/// The legacy case. +macro_rules! gate_legacy { + ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{ + if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) { + feature_warn(&$visitor.sess.parse_sess, sym::$feature, $span, $explain); + } + }}; } pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) { @@ -62,7 +66,7 @@ impl<'a> PostExpansionVisitor<'a> { match symbol_unescaped { // Stable sym::Rust | sym::C => {} - abi => gate_feature_post!( + abi => gate!( &self, const_extern_fn, span, @@ -113,14 +117,14 @@ impl<'a> PostExpansionVisitor<'a> { fn visit_ty(&mut self, ty: &ast::Ty) { if let ast::TyKind::ImplTrait(..) = ty.kind { if self.in_associated_ty { - gate_feature_post!( + gate!( &self.vis, impl_trait_in_assoc_type, ty.span, "`impl Trait` in associated types is unstable" ); } else { - gate_feature_post!( + gate!( &self.vis, type_alias_impl_trait, ty.span, @@ -172,7 +176,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { .. }) = attr_info { - gate_feature_fn!(self, has_feature, attr.span, *name, *descr); + gate_alt!(self, has_feature(&self.features), *name, attr.span, *descr); } // Check unstable flavors of the `#[doc]` attribute. if attr.has_name(sym::doc) { @@ -180,7 +184,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { macro_rules! gate_doc { ($($s:literal { $($name:ident => $feature:ident)* })*) => { $($(if nested_meta.has_name(sym::$name) { let msg = concat!("`#[doc(", stringify!($name), ")]` is ", $s); - gate_feature_post!(self, $feature, attr.span, msg); + gate!(self, $feature, attr.span, msg); })*)* }} @@ -204,7 +208,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { && !self.features.diagnostic_namespace { let msg = "`#[diagnostic]` attribute name space is experimental"; - gate_feature_post!(self, diagnostic_namespace, seg.ident.span, msg); + gate!(self, diagnostic_namespace, seg.ident.span, msg); } // Emit errors for non-staged-api crates. @@ -230,12 +234,11 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { ast::ItemKind::Fn(..) => { if attr::contains_name(&i.attrs, sym::start) { - gate_feature_post!( + gate!( &self, start, i.span, - "`#[start]` functions are experimental \ - and their signature may change \ + "`#[start]` functions are experimental and their signature may change \ over time" ); } @@ -245,7 +248,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { for attr in attr::filter_by_name(&i.attrs, sym::repr) { for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) { if item.has_name(sym::simd) { - gate_feature_post!( + gate!( &self, repr_simd, attr.span, @@ -258,7 +261,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, of_trait, .. }) => { if let &ast::ImplPolarity::Negative(span) = polarity { - gate_feature_post!( + gate!( &self, negative_impls, span.to(of_trait.as_ref().map_or(span, |t| t.path.span)), @@ -268,12 +271,12 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } if let ast::Defaultness::Default(_) = defaultness { - gate_feature_post!(&self, specialization, i.span, "specialization is unstable"); + gate!(&self, specialization, i.span, "specialization is unstable"); } } ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => { - gate_feature_post!( + gate!( &self, auto_traits, i.span, @@ -282,12 +285,12 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } ast::ItemKind::TraitAlias(..) => { - gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental"); + gate!(&self, trait_alias, i.span, "trait aliases are experimental"); } ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => { let msg = "`macro` is experimental"; - gate_feature_post!(&self, decl_macro, i.span, msg); + gate!(&self, decl_macro, i.span, msg); } ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => { @@ -306,7 +309,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { let link_name = attr::first_attr_value_str_by_name(&i.attrs, sym::link_name); let links_to_llvm = link_name.is_some_and(|val| val.as_str().starts_with("llvm.")); if links_to_llvm { - gate_feature_post!( + gate!( &self, link_llvm_intrinsics, i.span, @@ -315,7 +318,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } ast::ForeignItemKind::TyAlias(..) => { - gate_feature_post!(&self, extern_types, i.span, "extern types are experimental"); + gate!(&self, extern_types, i.span, "extern types are experimental"); } ast::ForeignItemKind::MacCall(..) => {} } @@ -331,7 +334,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { self.check_late_bound_lifetime_defs(&bare_fn_ty.generic_params); } ast::TyKind::Never => { - gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental"); + gate!(&self, never_type, ty.span, "the `!` type is experimental"); } _ => {} } @@ -364,7 +367,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_expr(&mut self, e: &'a ast::Expr) { match e.kind { ast::ExprKind::TryBlock(_) => { - gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental"); + gate!(&self, try_blocks, e.span, "`try` expression is experimental"); } _ => {} } @@ -380,7 +383,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { _ => pat, }; if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind { - gate_feature_post!( + gate!( &self, half_open_range_patterns_in_slices, pat.span, @@ -390,15 +393,10 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } PatKind::Box(..) => { - gate_feature_post!( - &self, - box_patterns, - pattern.span, - "box pattern syntax is experimental" - ); + gate!(&self, box_patterns, pattern.span, "box pattern syntax is experimental"); } PatKind::Range(_, Some(_), Spanned { node: RangeEnd::Excluded, .. }) => { - gate_feature_post!( + gate!( &self, exclusive_range_pattern, pattern.span, @@ -426,7 +424,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() { - gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable"); + gate!(&self, c_variadic, span, "C-variadic functions are unstable"); } visit::walk_fn(self, fn_kind) @@ -438,14 +436,14 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { && args.inputs.is_empty() && matches!(args.output, ast::FnRetTy::Default(..)) { - gate_feature_post!( + gate!( &self, return_type_notation, constraint.span, "return type notation is experimental" ); } else { - gate_feature_post!( + gate!( &self, associated_type_bounds, constraint.span, @@ -461,7 +459,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { ast::AssocItemKind::Fn(_) => true, ast::AssocItemKind::Type(box ast::TyAlias { ty, .. }) => { if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) { - gate_feature_post!( + gate!( &self, associated_type_defaults, i.span, @@ -477,11 +475,11 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { }; if let ast::Defaultness::Default(_) = i.kind.defaultness() { // Limit `min_specialization` to only specializing functions. - gate_feature_fn!( + gate_alt!( &self, - |x: &Features| x.specialization || (is_fn && x.min_specialization), - i.span, + self.features.specialization || (is_fn && self.features.min_specialization), sym::specialization, + i.span, "specialization is unstable" ); } @@ -496,17 +494,17 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { let spans = sess.parse_sess.gated_spans.spans.borrow(); macro_rules! gate_all { - ($gate:ident, $msg:literal, $help:literal) => { + ($gate:ident, $msg:literal) => { if let Some(spans) = spans.get(&sym::$gate) { for span in spans { - gate_feature_post!(&visitor, $gate, *span, $msg, $help); + gate!(&visitor, $gate, *span, $msg); } } }; - ($gate:ident, $msg:literal) => { + ($gate:ident, $msg:literal, $help:literal) => { if let Some(spans) = spans.get(&sym::$gate) { for span in spans { - gate_feature_post!(&visitor, $gate, *span, $msg); + gate!(&visitor, $gate, *span, $msg, $help); } } }; @@ -531,7 +529,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { gate_all!(more_qualified_paths, "usage of qualified paths in this context is experimental"); for &span in spans.get(&sym::yield_expr).iter().copied().flatten() { if !span.at_least_rust_2024() { - gate_feature_post!(&visitor, coroutines, span, "yield syntax is experimental"); + gate!(&visitor, coroutines, span, "yield syntax is experimental"); } } gate_all!(gen_blocks, "gen blocks are experimental"); @@ -565,7 +563,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { macro_rules! gate_all_legacy_dont_use { ($gate:ident, $msg:literal) => { for span in spans.get(&sym::$gate).unwrap_or(&vec![]) { - gate_feature_post!(future_incompatible; &visitor, $gate, *span, $msg); + gate_legacy!(&visitor, $gate, *span, $msg); } }; } From 499b3098f83d1d855169d14601d694c1c2a38963 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 30 Oct 2023 10:33:24 +1100 Subject: [PATCH 6/7] Fix a `FIXME`, by adding a `gate_multi` macro. Note that this adds the `span.allows_unstable` checking that this case previously lacked. --- compiler/rustc_ast_passes/src/feature_gate.rs | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 7dd6bfa13712b..3b0b731786161 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -30,7 +30,7 @@ macro_rules! gate { } /// The unusual case, where the `has_feature` condition is non-standard. -macro_rules! gate_alt { +macro_rules! gate_complex { ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr) => {{ if !$has_feature && !$span.allows_unstable($name) { feature_err(&$visitor.sess.parse_sess, $name, $span, $explain).emit(); @@ -38,6 +38,19 @@ macro_rules! gate_alt { }}; } +/// The case involving a multispan. +macro_rules! gate_multi { + ($visitor:expr, $feature:ident, $spans:expr, $explain:expr) => {{ + if !$visitor.features.$feature { + let spans: Vec<_> = + $spans.filter(|span| !span.allows_unstable(sym::$feature)).collect(); + if !spans.is_empty() { + feature_err(&$visitor.sess.parse_sess, sym::$feature, spans, $explain).emit(); + } + } + }}; +} + /// The legacy case. macro_rules! gate_legacy { ($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{ @@ -141,23 +154,16 @@ impl<'a> PostExpansionVisitor<'a> { fn check_late_bound_lifetime_defs(&self, params: &[ast::GenericParam]) { // Check only lifetime parameters are present and that the lifetime // parameters that are present have no bounds. - let non_lt_param_spans: Vec<_> = params - .iter() - .filter_map(|param| match param.kind { - ast::GenericParamKind::Lifetime { .. } => None, - _ => Some(param.ident.span), - }) - .collect(); - // FIXME: gate_feature_post doesn't really handle multispans... - if !non_lt_param_spans.is_empty() && !self.features.non_lifetime_binders { - feature_err( - &self.sess.parse_sess, - sym::non_lifetime_binders, - non_lt_param_spans, - crate::fluent_generated::ast_passes_forbidden_non_lifetime_param, - ) - .emit(); - } + let non_lt_param_spans = params.iter().filter_map(|param| match param.kind { + ast::GenericParamKind::Lifetime { .. } => None, + _ => Some(param.ident.span), + }); + gate_multi!( + &self, + non_lifetime_binders, + non_lt_param_spans, + crate::fluent_generated::ast_passes_forbidden_non_lifetime_param + ); for param in params { if !param.bounds.is_empty() { let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect(); From 5b391b01ce66240bfcdbd92ac45339f26db283c2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 30 Oct 2023 10:38:57 +1100 Subject: [PATCH 7/7] Test the multispan case in `tests.ui/bounds-lifetime.rs`. --- compiler/rustc_ast_passes/src/feature_gate.rs | 2 +- tests/ui/bounds-lifetime.rs | 2 +- tests/ui/bounds-lifetime.stderr | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 3b0b731786161..e1cf0a2589d3f 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -30,7 +30,7 @@ macro_rules! gate { } /// The unusual case, where the `has_feature` condition is non-standard. -macro_rules! gate_complex { +macro_rules! gate_alt { ($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr) => {{ if !$has_feature && !$span.allows_unstable($name) { feature_err(&$visitor.sess.parse_sess, $name, $span, $explain).emit(); diff --git a/tests/ui/bounds-lifetime.rs b/tests/ui/bounds-lifetime.rs index 31aa4011b9114..e3e635a4e8405 100644 --- a/tests/ui/bounds-lifetime.rs +++ b/tests/ui/bounds-lifetime.rs @@ -2,6 +2,6 @@ type A = for<'b, 'a: 'b> fn(); //~ ERROR lifetime bounds cannot be used in this type B = for<'b, 'a: 'b,> fn(); //~ ERROR lifetime bounds cannot be used in this context type C = for<'b, 'a: 'b +> fn(); //~ ERROR lifetime bounds cannot be used in this context type D = for<'a, T> fn(); //~ ERROR only lifetime parameters can be used in this context -type E = dyn for Fn(); //~ ERROR only lifetime parameters can be used in this context +type E = dyn for Fn(); //~ ERROR only lifetime parameters can be used in this context fn main() {} diff --git a/tests/ui/bounds-lifetime.stderr b/tests/ui/bounds-lifetime.stderr index f0bfe784cccc9..bbae835d87544 100644 --- a/tests/ui/bounds-lifetime.stderr +++ b/tests/ui/bounds-lifetime.stderr @@ -28,8 +28,8 @@ LL | type D = for<'a, T> fn(); error[E0658]: only lifetime parameters can be used in this context --> $DIR/bounds-lifetime.rs:5:18 | -LL | type E = dyn for Fn(); - | ^ +LL | type E = dyn for Fn(); + | ^ ^ | = note: see issue #108185 for more information = help: add `#![feature(non_lifetime_binders)]` to the crate attributes to enable