From 01b8e1bfe90357f7d6ec83798827f715ee7970f3 Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 10 Dec 2024 09:36:51 -0300 Subject: [PATCH 1/2] [generic_assert] Avoid constant environments --- compiler/rustc_ast/src/ast.rs | 1 + compiler/rustc_ast/src/mut_visit.rs | 2 +- compiler/rustc_ast/src/visit.rs | 2 +- compiler/rustc_builtin_macros/src/asm.rs | 3 +++ compiler/rustc_builtin_macros/src/assert.rs | 6 +++-- .../src/assert/context.rs | 12 +++++++--- compiler/rustc_builtin_macros/src/cfg.rs | 1 + .../rustc_builtin_macros/src/compile_error.rs | 1 + compiler/rustc_builtin_macros/src/concat.rs | 1 + .../rustc_builtin_macros/src/concat_bytes.rs | 1 + .../rustc_builtin_macros/src/concat_idents.rs | 1 + .../rustc_builtin_macros/src/edition_panic.rs | 8 +++++-- compiler/rustc_builtin_macros/src/env.rs | 2 ++ compiler/rustc_builtin_macros/src/format.rs | 2 ++ .../rustc_builtin_macros/src/log_syntax.rs | 1 + .../rustc_builtin_macros/src/pattern_type.rs | 1 + .../rustc_builtin_macros/src/source_util.rs | 8 +++++++ .../rustc_builtin_macros/src/trace_macros.rs | 1 + compiler/rustc_expand/src/base.rs | 11 ++++++--- compiler/rustc_expand/src/build.rs | 3 +++ compiler/rustc_expand/src/expand.rs | 14 +++++++++-- compiler/rustc_expand/src/mbe/macro_rules.rs | 2 ++ compiler/rustc_expand/src/placeholders.rs | 1 + compiler/rustc_expand/src/proc_macro.rs | 1 + compiler/rustc_parse/src/parser/expr.rs | 6 ++++- compiler/rustc_parse/src/parser/item.rs | 24 +++++++++++++++---- compiler/rustc_parse/src/parser/mod.rs | 14 +++++++++-- compiler/rustc_parse/src/parser/pat.rs | 2 +- compiler/rustc_parse/src/parser/path.rs | 12 ++++++---- compiler/rustc_parse/src/parser/stmt.rs | 2 +- compiler/rustc_parse/src/parser/ty.rs | 6 ++++- .../const-environments.rs | 19 +++++++++++++++ .../const-environments.stderr | 11 +++++++++ 33 files changed, 152 insertions(+), 30 deletions(-) create mode 100644 tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.rs create mode 100644 tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.stderr diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 56b20e0ad8938..3e911ddd6687b 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -1715,6 +1715,7 @@ pub enum ClosureBinder { /// is being invoked, and the `args` are arguments passed to it. #[derive(Clone, Encodable, Decodable, Debug)] pub struct MacCall { + pub is_in_const_env: bool, pub path: Path, pub args: P, } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 0aceed45028a1..037538fc253b0 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -714,7 +714,7 @@ fn walk_attribute(vis: &mut T, attr: &mut Attribute) { } fn walk_mac(vis: &mut T, mac: &mut MacCall) { - let MacCall { path, args } = mac; + let MacCall { is_in_const_env: _, path, args } = mac; vis.visit_path(path); visit_delim_args(vis, args); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 718397e8ca004..80e01cc8c960e 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -1002,7 +1002,7 @@ pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V: } pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) -> V::Result { - let MacCall { path, args: _ } = mac; + let MacCall { is_in_const_env: _, path, args: _ } = mac; visitor.visit_path(path, DUMMY_NODE_ID) } diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index 14ac3cd74e884..a6f6ba237b5b6 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -814,6 +814,7 @@ fn expand_preparsed_asm( pub(super) fn expand_asm<'cx>( ecx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { @@ -843,6 +844,7 @@ pub(super) fn expand_asm<'cx>( pub(super) fn expand_naked_asm<'cx>( ecx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { @@ -873,6 +875,7 @@ pub(super) fn expand_naked_asm<'cx>( pub(super) fn expand_global_asm<'cx>( ecx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 599b180f8793d..9c445301ae83d 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -17,6 +17,7 @@ use crate::errors; pub(crate) fn expand_assert<'cx>( cx: &'cx mut ExtCtxt<'_>, + is_in_const_env: bool, span: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { @@ -56,6 +57,7 @@ pub(crate) fn expand_assert<'cx>( let then = cx.expr( call_site_span, ExprKind::MacCall(P(MacCall { + is_in_const_env, path: panic_path(), args: P(DelimArgs { dspan: DelimSpan::from_single(call_site_span), @@ -69,8 +71,8 @@ pub(crate) fn expand_assert<'cx>( // If `generic_assert` is enabled, generates rich captured outputs // // FIXME(c410-f3r) See https://github.com/rust-lang/rust/issues/96949 - else if cx.ecfg.features.generic_assert() { - context::Context::new(cx, call_site_span).build(cond_expr, panic_path()) + else if cx.ecfg.features.generic_assert() && !is_in_const_env { + context::Context::new(cx, call_site_span).build(cond_expr, is_in_const_env, panic_path()) } // If `generic_assert` is not enabled, only outputs a literal "assertion failed: ..." // string diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs index 70fa4d00c0f39..1e26e173c793a 100644 --- a/compiler/rustc_builtin_macros/src/assert/context.rs +++ b/compiler/rustc_builtin_macros/src/assert/context.rs @@ -71,11 +71,16 @@ impl<'cx, 'a> Context<'cx, 'a> { /// } /// } /// ``` - pub(super) fn build(mut self, mut cond_expr: P, panic_path: Path) -> P { + pub(super) fn build( + mut self, + mut cond_expr: P, + is_in_const_env: bool, + panic_path: Path, + ) -> P { let expr_str = pprust::expr_to_string(&cond_expr); self.manage_cond_expr(&mut cond_expr); let initial_imports = self.build_initial_imports(); - let panic = self.build_panic(&expr_str, panic_path); + let panic = self.build_panic(&expr_str, is_in_const_env, panic_path); let cond_expr_with_unlikely = self.build_unlikely(cond_expr); let Self { best_case_captures, capture_decls, cx, local_bind_decls, span, .. } = self; @@ -147,7 +152,7 @@ impl<'cx, 'a> Context<'cx, 'a> { /// __capture0, /// ... /// ); - fn build_panic(&self, expr_str: &str, panic_path: Path) -> P { + fn build_panic(&self, expr_str: &str, is_in_const_env: bool, panic_path: Path) -> P { let escaped_expr_str = escape_to_fmt(expr_str); let initial = [ TokenTree::token_joint( @@ -179,6 +184,7 @@ impl<'cx, 'a> Context<'cx, 'a> { self.cx.expr( self.span, ExprKind::MacCall(P(MacCall { + is_in_const_env, path: panic_path, args: P(DelimArgs { dspan: DelimSpan::from_single(self.span), diff --git a/compiler/rustc_builtin_macros/src/cfg.rs b/compiler/rustc_builtin_macros/src/cfg.rs index 15993dbf5ecc3..31dcdc0ed396e 100644 --- a/compiler/rustc_builtin_macros/src/cfg.rs +++ b/compiler/rustc_builtin_macros/src/cfg.rs @@ -13,6 +13,7 @@ use crate::errors; pub(crate) fn expand_cfg( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { diff --git a/compiler/rustc_builtin_macros/src/compile_error.rs b/compiler/rustc_builtin_macros/src/compile_error.rs index 7fc4b437c1d8d..67860896728db 100644 --- a/compiler/rustc_builtin_macros/src/compile_error.rs +++ b/compiler/rustc_builtin_macros/src/compile_error.rs @@ -8,6 +8,7 @@ use crate::util::get_single_str_from_tts; pub(crate) fn expand_compile_error<'cx>( cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_builtin_macros/src/concat.rs b/compiler/rustc_builtin_macros/src/concat.rs index a28801f66dd10..f363b80223801 100644 --- a/compiler/rustc_builtin_macros/src/concat.rs +++ b/compiler/rustc_builtin_macros/src/concat.rs @@ -9,6 +9,7 @@ use crate::util::get_exprs_from_tts; pub(crate) fn expand_concat( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: rustc_span::Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { diff --git a/compiler/rustc_builtin_macros/src/concat_bytes.rs b/compiler/rustc_builtin_macros/src/concat_bytes.rs index 456f2b9ab31de..bc86425335a65 100644 --- a/compiler/rustc_builtin_macros/src/concat_bytes.rs +++ b/compiler/rustc_builtin_macros/src/concat_bytes.rs @@ -112,6 +112,7 @@ fn handle_array_element( pub(crate) fn expand_concat_bytes( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { diff --git a/compiler/rustc_builtin_macros/src/concat_idents.rs b/compiler/rustc_builtin_macros/src/concat_idents.rs index b459cc3007d37..648371087709a 100644 --- a/compiler/rustc_builtin_macros/src/concat_idents.rs +++ b/compiler/rustc_builtin_macros/src/concat_idents.rs @@ -10,6 +10,7 @@ use crate::errors; pub(crate) fn expand_concat_idents<'cx>( cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_builtin_macros/src/edition_panic.rs b/compiler/rustc_builtin_macros/src/edition_panic.rs index c4164a7db9a47..c4e9ada6fa0cd 100644 --- a/compiler/rustc_builtin_macros/src/edition_panic.rs +++ b/compiler/rustc_builtin_macros/src/edition_panic.rs @@ -18,11 +18,12 @@ use rustc_span::symbol::sym; /// one we're expanding from. pub(crate) fn expand_panic<'cx>( cx: &'cx mut ExtCtxt<'_>, + is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { let mac = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 }; - expand(mac, cx, sp, tts) + expand(is_in_const_env, mac, cx, sp, tts) } /// This expands to either @@ -31,14 +32,16 @@ pub(crate) fn expand_panic<'cx>( /// depending on the edition. pub(crate) fn expand_unreachable<'cx>( cx: &'cx mut ExtCtxt<'_>, + is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { let mac = if use_panic_2021(sp) { sym::unreachable_2021 } else { sym::unreachable_2015 }; - expand(mac, cx, sp, tts) + expand(is_in_const_env, mac, cx, sp, tts) } fn expand<'cx>( + is_in_const_env: bool, mac: rustc_span::Symbol, cx: &'cx ExtCtxt<'_>, sp: Span, @@ -50,6 +53,7 @@ fn expand<'cx>( cx.expr( sp, ExprKind::MacCall(P(MacCall { + is_in_const_env, path: Path { span: sp, segments: cx diff --git a/compiler/rustc_builtin_macros/src/env.rs b/compiler/rustc_builtin_macros/src/env.rs index 43e2bf1796fc7..f7d55f1ed835f 100644 --- a/compiler/rustc_builtin_macros/src/env.rs +++ b/compiler/rustc_builtin_macros/src/env.rs @@ -29,6 +29,7 @@ fn lookup_env<'cx>(cx: &'cx ExtCtxt<'_>, var: Symbol) -> Result( cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { @@ -89,6 +90,7 @@ pub(crate) fn expand_option_env<'cx>( pub(crate) fn expand_env<'cx>( cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index 32730ac3867f5..5f0e2d0fd790b 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -1015,6 +1015,7 @@ fn expand_format_args_impl<'cx>( pub(crate) fn expand_format_args<'cx>( ecx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { @@ -1023,6 +1024,7 @@ pub(crate) fn expand_format_args<'cx>( pub(crate) fn expand_format_args_nl<'cx>( ecx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_builtin_macros/src/log_syntax.rs b/compiler/rustc_builtin_macros/src/log_syntax.rs index 205f21ae7c9d3..d04c75a4fa4f3 100644 --- a/compiler/rustc_builtin_macros/src/log_syntax.rs +++ b/compiler/rustc_builtin_macros/src/log_syntax.rs @@ -4,6 +4,7 @@ use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult pub(crate) fn expand_log_syntax<'cx>( _cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: rustc_span::Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_builtin_macros/src/pattern_type.rs b/compiler/rustc_builtin_macros/src/pattern_type.rs index 90b5f097b32b3..fec4f188d3c15 100644 --- a/compiler/rustc_builtin_macros/src/pattern_type.rs +++ b/compiler/rustc_builtin_macros/src/pattern_type.rs @@ -7,6 +7,7 @@ use rustc_span::{Span, sym}; pub(crate) fn expand<'cx>( cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_builtin_macros/src/source_util.rs b/compiler/rustc_builtin_macros/src/source_util.rs index 946fbe918e6a6..c8b2d85b0fa38 100644 --- a/compiler/rustc_builtin_macros/src/source_util.rs +++ b/compiler/rustc_builtin_macros/src/source_util.rs @@ -32,6 +32,7 @@ use crate::util::{ /// line!(): expands to the current line number pub(crate) fn expand_line( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { @@ -47,6 +48,7 @@ pub(crate) fn expand_line( /* column!(): expands to the current column number */ pub(crate) fn expand_column( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { @@ -64,6 +66,7 @@ pub(crate) fn expand_column( /// out if we wanted. pub(crate) fn expand_file( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { @@ -85,6 +88,7 @@ pub(crate) fn expand_file( pub(crate) fn expand_stringify( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { @@ -95,6 +99,7 @@ pub(crate) fn expand_stringify( pub(crate) fn expand_mod( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { @@ -111,6 +116,7 @@ pub(crate) fn expand_mod( /// unhygienically. pub(crate) fn expand_include<'cx>( cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'cx> { @@ -192,6 +198,7 @@ pub(crate) fn expand_include<'cx>( /// `include_str!`: read the given file, insert it as a literal string expr pub(crate) fn expand_include_str( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { @@ -221,6 +228,7 @@ pub(crate) fn expand_include_str( pub(crate) fn expand_include_bytes( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tts: TokenStream, ) -> MacroExpanderResult<'static> { diff --git a/compiler/rustc_builtin_macros/src/trace_macros.rs b/compiler/rustc_builtin_macros/src/trace_macros.rs index e624d1da66bd5..ee1f00c330862 100644 --- a/compiler/rustc_builtin_macros/src/trace_macros.rs +++ b/compiler/rustc_builtin_macros/src/trace_macros.rs @@ -7,6 +7,7 @@ use crate::errors; pub(crate) fn expand_trace_macros( cx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, tt: TokenStream, ) -> MacroExpanderResult<'static> { diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index bed500c303242..8bd49be38e60b 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -280,6 +280,7 @@ pub trait BangProcMacro { fn expand<'cx>( &self, ecx: &'cx mut ExtCtxt<'_>, + is_in_const_env: bool, span: Span, ts: TokenStream, ) -> Result; @@ -292,6 +293,7 @@ where fn expand<'cx>( &self, _ecx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, _span: Span, ts: TokenStream, ) -> Result { @@ -331,6 +333,7 @@ pub trait TTMacroExpander { fn expand<'cx>( &self, ecx: &'cx mut ExtCtxt<'_>, + is_in_const_env: bool, span: Span, input: TokenStream, ) -> MacroExpanderResult<'cx>; @@ -339,19 +342,20 @@ pub trait TTMacroExpander { pub type MacroExpanderResult<'cx> = ExpandResult, ()>; pub type MacroExpanderFn = - for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>; + for<'cx> fn(&'cx mut ExtCtxt<'_>, bool, Span, TokenStream) -> MacroExpanderResult<'cx>; impl TTMacroExpander for F where - F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>, + F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, bool, Span, TokenStream) -> MacroExpanderResult<'cx>, { fn expand<'cx>( &self, ecx: &'cx mut ExtCtxt<'_>, + is_in_const_env: bool, span: Span, input: TokenStream, ) -> MacroExpanderResult<'cx> { - self(ecx, span, input) + self(ecx, is_in_const_env, span, input) } } @@ -901,6 +905,7 @@ impl SyntaxExtension { pub fn dummy_bang(edition: Edition) -> SyntaxExtension { fn expander<'cx>( cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, span: Span, _: TokenStream, ) -> MacroExpanderResult<'cx> { diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index a673e2e3250d5..4f3122889e63a 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -53,12 +53,14 @@ impl<'a> ExtCtxt<'a> { pub fn macro_call( &self, + is_in_const_env: bool, span: Span, path: ast::Path, delim: ast::token::Delimiter, tokens: ast::tokenstream::TokenStream, ) -> P { P(ast::MacCall { + is_in_const_env, path, args: P(ast::DelimArgs { dspan: ast::tokenstream::DelimSpan { open: span, close: span }, @@ -435,6 +437,7 @@ impl<'a> ExtCtxt<'a> { self.expr_macro_call( span, self.macro_call( + false, span, self.path_global( span, diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 19c2d466f7ca6..6a12de7b7f2a5 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -676,7 +676,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> { ExpandResult::Ready(match invoc.kind { InvocationKind::Bang { mac, span } => match ext { SyntaxExtensionKind::Bang(expander) => { - match expander.expand(self.cx, span, mac.args.tokens.clone()) { + match expander.expand( + self.cx, + mac.is_in_const_env, + span, + mac.args.tokens.clone(), + ) { Ok(tok_result) => { self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span) } @@ -684,7 +689,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } } SyntaxExtensionKind::LegacyBang(expander) => { - let tok_result = match expander.expand(self.cx, span, mac.args.tokens.clone()) { + let tok_result = match expander.expand( + self.cx, + mac.is_in_const_env, + span, + mac.args.tokens.clone(), + ) { ExpandResult::Ready(tok_result) => tok_result, ExpandResult::Retry(_) => { // retry the original diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index a373c753cc114..55853d29dc44b 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -111,6 +111,7 @@ impl TTMacroExpander for MacroRulesMacroExpander { fn expand<'cx>( &self, cx: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, sp: Span, input: TokenStream, ) -> MacroExpanderResult<'cx> { @@ -134,6 +135,7 @@ impl TTMacroExpander for DummyExpander { fn expand<'cx>( &self, _: &'cx mut ExtCtxt<'_>, + _is_in_const_env: bool, span: Span, _: TokenStream, ) -> ExpandResult, ()> { diff --git a/compiler/rustc_expand/src/placeholders.rs b/compiler/rustc_expand/src/placeholders.rs index f044d964f1358..09b2698d3b217 100644 --- a/compiler/rustc_expand/src/placeholders.rs +++ b/compiler/rustc_expand/src/placeholders.rs @@ -18,6 +18,7 @@ pub(crate) fn placeholder( ) -> AstFragment { fn mac_placeholder() -> P { P(ast::MacCall { + is_in_const_env: false, path: ast::Path { span: DUMMY_SP, segments: ThinVec::new(), tokens: None }, args: P(ast::DelimArgs { dspan: ast::tokenstream::DelimSpan::dummy(), diff --git a/compiler/rustc_expand/src/proc_macro.rs b/compiler/rustc_expand/src/proc_macro.rs index dca0516f9f3b3..fe9a5450d653d 100644 --- a/compiler/rustc_expand/src/proc_macro.rs +++ b/compiler/rustc_expand/src/proc_macro.rs @@ -46,6 +46,7 @@ impl base::BangProcMacro for BangProcMacro { fn expand( &self, ecx: &mut ExtCtxt<'_>, + _is_in_const_env: bool, span: Span, input: TokenStream, ) -> Result { diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 4430d2d14313b..4c1b2ffd42f41 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1614,7 +1614,11 @@ impl<'a> Parser<'a> { self.dcx().emit_err(errors::MacroInvocationWithQualifiedPath(path.span)); } let lo = path.span; - let mac = P(MacCall { path, args: self.parse_delim_args()? }); + let mac = P(MacCall { + is_in_const_env: self.is_in_const_env, + path, + args: self.parse_delim_args()?, + }); (lo.to(self.prev_token.span), ExprKind::MacCall(mac)) } else if self.check(&token::OpenDelim(Delimiter::Brace)) && let Some(expr) = self.maybe_parse_struct_expr(&qself, &path) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 26e81b7676bb1..f534d2e3b36fc 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -241,11 +241,14 @@ impl<'a> Parser<'a> { // CONST ITEM if self.token.is_keyword(kw::Impl) { // recover from `const impl`, suggest `impl const` - self.recover_const_impl(const_span, attrs, def_())? + self.with_const_management(|this| { + this.recover_const_impl(const_span, attrs, def_()) + })? } else { self.recover_const_mut(const_span); self.recover_missing_kw_before_item()?; - let (ident, generics, ty, expr) = self.parse_const_item()?; + let tuple = self.with_const_management(|this| this.parse_const_item())?; + let (ident, generics, ty, expr) = tuple; ( ident, ItemKind::Const(Box::new(ConstItem { @@ -486,7 +489,7 @@ impl<'a> Parser<'a> { Ok(args) => { self.eat_semi_for_macro_if_needed(&args); self.complain_if_pub_macro(vis, false); - Ok(MacCall { path, args }) + Ok(MacCall { is_in_const_env: self.is_in_const_env, path, args }) } Err(mut err) => { @@ -2383,8 +2386,19 @@ impl<'a> Parser<'a> { let mut sig_hi = self.prev_token.span; // Either `;` or `{ ... }`. - let body = - self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body, fn_params_end)?; + let body = if let Const::Yes(_) = header.constness { + self.with_const_management(|this| { + this.parse_fn_body( + attrs, + &ident, + &mut sig_hi, + fn_parse_mode.req_body, + fn_params_end, + ) + })? + } else { + self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body, fn_params_end)? + }; let fn_sig_span = sig_lo.to(sig_hi); Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, body)) } diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 0ed8d152d2d34..bc6423d1fa2e5 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -185,12 +185,14 @@ pub struct Parser<'a> { /// Whether the parser is allowed to do recovery. /// This is disabled when parsing macro arguments, see #103534 recovery: Recovery, + /// If the current parsing is within a constant environment + is_in_const_env: bool, } // This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure // it doesn't unintentionally get bigger. #[cfg(target_pointer_width = "64")] -rustc_data_structures::static_assert_size!(Parser<'_>, 288); +rustc_data_structures::static_assert_size!(Parser<'_>, 296); /// Stores span information about a closure. #[derive(Clone, Debug)] @@ -481,6 +483,7 @@ impl<'a> Parser<'a> { }, current_closure: None, recovery: Recovery::Allowed, + is_in_const_env: false, }; // Make parser point to the first token. @@ -1320,7 +1323,7 @@ impl<'a> Parser<'a> { self.psess.gated_spans.gate(sym::inline_const_pat, span); } self.expect_keyword(kw::Const)?; - let (attrs, blk) = self.parse_inner_attrs_and_block()?; + let (attrs, blk) = self.with_const_management(|this| this.parse_inner_attrs_and_block())?; let anon_const = AnonConst { id: DUMMY_NODE_ID, value: self.mk_expr(blk.span, ExprKind::Block(blk, None)), @@ -1650,6 +1653,13 @@ impl<'a> Parser<'a> { pub fn approx_token_stream_pos(&self) -> u32 { self.num_bump_calls } + + fn with_const_management(&mut self, f: impl FnOnce(&mut Self) -> T) -> T { + self.is_in_const_env = true; + let rslt = f(self); + self.is_in_const_env = false; + rslt + } } pub(crate) fn make_unclosed_delims_error( diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index bb976e092bf4c..cb48853af1af5 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -1098,7 +1098,7 @@ impl<'a> Parser<'a> { fn parse_pat_mac_invoc(&mut self, path: Path) -> PResult<'a, PatKind> { self.bump(); let args = self.parse_delim_args()?; - let mac = P(MacCall { path, args }); + let mac = P(MacCall { is_in_const_env: self.is_in_const_env, path, args }); Ok(PatKind::MacCall(mac)) } diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 2f19a9b6b20b4..96da971c3b60f 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -843,11 +843,13 @@ impl<'a> Parser<'a> { /// the caller. pub(super) fn parse_const_arg(&mut self) -> PResult<'a, AnonConst> { // Parse const argument. - let value = if let token::OpenDelim(Delimiter::Brace) = self.token.kind { - self.parse_expr_block(None, self.token.span, BlockCheckMode::Default)? - } else { - self.handle_unambiguous_unbraced_const_arg()? - }; + let value = self.with_const_management(|this| { + if let token::OpenDelim(Delimiter::Brace) = this.token.kind { + this.parse_expr_block(None, this.token.span, BlockCheckMode::Default) + } else { + this.handle_unambiguous_unbraced_const_arg() + } + })?; Ok(AnonConst { id: ast::DUMMY_NODE_ID, value }) } diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 190cd9ed0610f..729a298177c75 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -226,7 +226,7 @@ impl<'a> Parser<'a> { _ => MacStmtStyle::NoBraces, }; - let mac = P(MacCall { path, args }); + let mac = P(MacCall { is_in_const_env: self.is_in_const_env, path, args }); let kind = if (style == MacStmtStyle::Braces && self.token != token::Dot diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 505586e74f11f..b0e1a87062344 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -758,7 +758,11 @@ impl<'a> Parser<'a> { let path = self.parse_path_inner(PathStyle::Type, ty_generics)?; if self.eat(&token::Not) { // Macro invocation in type position - Ok(TyKind::MacCall(P(MacCall { path, args: self.parse_delim_args()? }))) + Ok(TyKind::MacCall(P(MacCall { + is_in_const_env: self.is_in_const_env, + path, + args: self.parse_delim_args()?, + }))) } else if allow_plus == AllowPlus::Yes && self.check_plus() { // `Trait1 + Trait2 + 'a` self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true) diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.rs new file mode 100644 index 0000000000000..2a3cacde506a1 --- /dev/null +++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.rs @@ -0,0 +1,19 @@ +#![feature(core_intrinsics, generic_assert)] + +const _: () = { + let foo = 1; + assert!(foo == 3); + //~^ERROR: evaluation of constant value failed +}; + +fn main() { + const { + let foo = 1; + assert!(foo == 3); + } + + const fn bar() { + let foo = 1; + assert!(foo == 3); + } +} diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.stderr b/tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.stderr new file mode 100644 index 0000000000000..a680b707e329d --- /dev/null +++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/const-environments.stderr @@ -0,0 +1,11 @@ +error[E0080]: evaluation of constant value failed + --> $DIR/const-environments.rs:5:5 + | +LL | assert!(foo == 3); + | ^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: foo == 3', $DIR/const-environments.rs:5:5 + | + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0080`. From b6501c58e638e0529ca9c904de5ef0ffc8d26f2b Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 10 Dec 2024 10:39:58 -0300 Subject: [PATCH 2/2] Remove crash --- tests/crashes/123629.rs | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 tests/crashes/123629.rs diff --git a/tests/crashes/123629.rs b/tests/crashes/123629.rs deleted file mode 100644 index 615323218067d..0000000000000 --- a/tests/crashes/123629.rs +++ /dev/null @@ -1,10 +0,0 @@ -//@ known-bug: #123629 -#![feature(generic_assert)] - -fn foo() -where - for ():, -{ -} - -fn main() {}