From a06ebec7ac1534ad1c4acd9a27c04dcbc0bca50d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Feb 2024 14:56:41 +0100 Subject: [PATCH] Correctly check `never_type` feature gating --- compiler/rustc_ast_passes/src/feature_gate.rs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 82236d2e30678..8bb32bf5738ee 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -59,7 +59,7 @@ macro_rules! gate_legacy { } pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) { - PostExpansionVisitor { sess, features }.visit_attribute(attr) + PostExpansionVisitor { sess, features, is_in_generics: false }.visit_attribute(attr) } struct PostExpansionVisitor<'a> { @@ -67,6 +67,7 @@ struct PostExpansionVisitor<'a> { // `sess` contains a `Features`, but this might not be that one. features: &'a Features, + is_in_generics: bool, } impl<'a> PostExpansionVisitor<'a> { @@ -349,17 +350,20 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { _ => {} } } + let prev = std::mem::replace(&mut self.is_in_generics, true); visit::walk_generics(self, g); + self.is_in_generics = prev; } fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) { - if let ast::FnRetTy::Ty(output_ty) = ret_ty { - if let ast::TyKind::Never = output_ty.kind { - // Do nothing. - } else { - self.visit_ty(output_ty) - } + if let ast::FnRetTy::Ty(output_ty) = ret_ty + && let ast::TyKind::Never = output_ty.kind + && !self.is_in_generics + { + // Do nothing. + return; } + visit::walk_fn_ret_ty(self, ret_ty); } fn visit_expr(&mut self, e: &'a ast::Expr) { @@ -489,7 +493,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { maybe_stage_features(sess, features, krate); check_incompatible_features(sess, features); - let mut visitor = PostExpansionVisitor { sess, features }; + let mut visitor = PostExpansionVisitor { sess, features, is_in_generics: false }; let spans = sess.parse_sess.gated_spans.spans.borrow(); macro_rules! gate_all {