Skip to content

Commit a7d860c

Browse files
committed
Auto merge of rust-lang#134126 - c410-f3r:hocus-pocus, r=<try>
[`generic_assert`] Avoid constant environments cc rust-lang#44838 This PR is a work in progress. Requesting an early perf run to evaluate possible impacts. The `generic_assert` feature captures variables for printing purposes. ```rust fn foo() { let elem = 1i32; assert!(&elem); } # Expansion fn foo() { let elem = 1i32; { use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable}; let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if (!&*__local_bind0) { (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); { ::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n", __capture0)); } } }; } ``` The problem is that such a thing is complicated in constant environments. At the current time only strings are allowed and a full support parity with non-constant environments is not, as far as I can tell, visible in the foreseen future. ```rust fn foo() { // !!! ERROR !!! const { let elem = 1i32; assert!(&elem); } } ``` Therefore, `generic_assert` will not be triggered in constant environment through an `is_in_const_env` variable flag created in the `rustc_parse` crate.
2 parents b597d2a + 01b8e1b commit a7d860c

33 files changed

+152
-30
lines changed

compiler/rustc_ast/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ pub enum ClosureBinder {
17181718
/// is being invoked, and the `args` are arguments passed to it.
17191719
#[derive(Clone, Encodable, Decodable, Debug)]
17201720
pub struct MacCall {
1721+
pub is_in_const_env: bool,
17211722
pub path: Path,
17221723
pub args: P<DelimArgs>,
17231724
}

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ fn walk_attribute<T: MutVisitor>(vis: &mut T, attr: &mut Attribute) {
711711
}
712712

713713
fn walk_mac<T: MutVisitor>(vis: &mut T, mac: &mut MacCall) {
714-
let MacCall { path, args } = mac;
714+
let MacCall { is_in_const_env: _, path, args } = mac;
715715
vis.visit_path(path);
716716
visit_delim_args(vis, args);
717717
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V:
10081008
}
10091009

10101010
pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) -> V::Result {
1011-
let MacCall { path, args: _ } = mac;
1011+
let MacCall { is_in_const_env: _, path, args: _ } = mac;
10121012
visitor.visit_path(path, DUMMY_NODE_ID)
10131013
}
10141014

compiler/rustc_builtin_macros/src/asm.rs

+3
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ fn expand_preparsed_asm(
814814

815815
pub(super) fn expand_asm<'cx>(
816816
ecx: &'cx mut ExtCtxt<'_>,
817+
_is_in_const_env: bool,
817818
sp: Span,
818819
tts: TokenStream,
819820
) -> MacroExpanderResult<'cx> {
@@ -843,6 +844,7 @@ pub(super) fn expand_asm<'cx>(
843844

844845
pub(super) fn expand_naked_asm<'cx>(
845846
ecx: &'cx mut ExtCtxt<'_>,
847+
_is_in_const_env: bool,
846848
sp: Span,
847849
tts: TokenStream,
848850
) -> MacroExpanderResult<'cx> {
@@ -873,6 +875,7 @@ pub(super) fn expand_naked_asm<'cx>(
873875

874876
pub(super) fn expand_global_asm<'cx>(
875877
ecx: &'cx mut ExtCtxt<'_>,
878+
_is_in_const_env: bool,
876879
sp: Span,
877880
tts: TokenStream,
878881
) -> MacroExpanderResult<'cx> {

compiler/rustc_builtin_macros/src/assert.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::errors;
1717

1818
pub(crate) fn expand_assert<'cx>(
1919
cx: &'cx mut ExtCtxt<'_>,
20+
is_in_const_env: bool,
2021
span: Span,
2122
tts: TokenStream,
2223
) -> MacroExpanderResult<'cx> {
@@ -56,6 +57,7 @@ pub(crate) fn expand_assert<'cx>(
5657
let then = cx.expr(
5758
call_site_span,
5859
ExprKind::MacCall(P(MacCall {
60+
is_in_const_env,
5961
path: panic_path(),
6062
args: P(DelimArgs {
6163
dspan: DelimSpan::from_single(call_site_span),
@@ -69,8 +71,8 @@ pub(crate) fn expand_assert<'cx>(
6971
// If `generic_assert` is enabled, generates rich captured outputs
7072
//
7173
// FIXME(c410-f3r) See https://github.com/rust-lang/rust/issues/96949
72-
else if cx.ecfg.features.generic_assert() {
73-
context::Context::new(cx, call_site_span).build(cond_expr, panic_path())
74+
else if cx.ecfg.features.generic_assert() && !is_in_const_env {
75+
context::Context::new(cx, call_site_span).build(cond_expr, is_in_const_env, panic_path())
7476
}
7577
// If `generic_assert` is not enabled, only outputs a literal "assertion failed: ..."
7678
// string

compiler/rustc_builtin_macros/src/assert/context.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ impl<'cx, 'a> Context<'cx, 'a> {
7171
/// }
7272
/// }
7373
/// ```
74-
pub(super) fn build(mut self, mut cond_expr: P<Expr>, panic_path: Path) -> P<Expr> {
74+
pub(super) fn build(
75+
mut self,
76+
mut cond_expr: P<Expr>,
77+
is_in_const_env: bool,
78+
panic_path: Path,
79+
) -> P<Expr> {
7580
let expr_str = pprust::expr_to_string(&cond_expr);
7681
self.manage_cond_expr(&mut cond_expr);
7782
let initial_imports = self.build_initial_imports();
78-
let panic = self.build_panic(&expr_str, panic_path);
83+
let panic = self.build_panic(&expr_str, is_in_const_env, panic_path);
7984
let cond_expr_with_unlikely = self.build_unlikely(cond_expr);
8085

8186
let Self { best_case_captures, capture_decls, cx, local_bind_decls, span, .. } = self;
@@ -147,7 +152,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
147152
/// __capture0,
148153
/// ...
149154
/// );
150-
fn build_panic(&self, expr_str: &str, panic_path: Path) -> P<Expr> {
155+
fn build_panic(&self, expr_str: &str, is_in_const_env: bool, panic_path: Path) -> P<Expr> {
151156
let escaped_expr_str = escape_to_fmt(expr_str);
152157
let initial = [
153158
TokenTree::token_joint(
@@ -179,6 +184,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
179184
self.cx.expr(
180185
self.span,
181186
ExprKind::MacCall(P(MacCall {
187+
is_in_const_env,
182188
path: panic_path,
183189
args: P(DelimArgs {
184190
dspan: DelimSpan::from_single(self.span),

compiler/rustc_builtin_macros/src/cfg.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::errors;
1313

1414
pub(crate) fn expand_cfg(
1515
cx: &mut ExtCtxt<'_>,
16+
_is_in_const_env: bool,
1617
sp: Span,
1718
tts: TokenStream,
1819
) -> MacroExpanderResult<'static> {

compiler/rustc_builtin_macros/src/compile_error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::util::get_single_str_from_tts;
88

99
pub(crate) fn expand_compile_error<'cx>(
1010
cx: &'cx mut ExtCtxt<'_>,
11+
_is_in_const_env: bool,
1112
sp: Span,
1213
tts: TokenStream,
1314
) -> MacroExpanderResult<'cx> {

compiler/rustc_builtin_macros/src/concat.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::util::get_exprs_from_tts;
99

1010
pub(crate) fn expand_concat(
1111
cx: &mut ExtCtxt<'_>,
12+
_is_in_const_env: bool,
1213
sp: rustc_span::Span,
1314
tts: TokenStream,
1415
) -> MacroExpanderResult<'static> {

compiler/rustc_builtin_macros/src/concat_bytes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn handle_array_element(
112112

113113
pub(crate) fn expand_concat_bytes(
114114
cx: &mut ExtCtxt<'_>,
115+
_is_in_const_env: bool,
115116
sp: Span,
116117
tts: TokenStream,
117118
) -> MacroExpanderResult<'static> {

compiler/rustc_builtin_macros/src/concat_idents.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::errors;
1010

1111
pub(crate) fn expand_concat_idents<'cx>(
1212
cx: &'cx mut ExtCtxt<'_>,
13+
_is_in_const_env: bool,
1314
sp: Span,
1415
tts: TokenStream,
1516
) -> MacroExpanderResult<'cx> {

compiler/rustc_builtin_macros/src/edition_panic.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ use rustc_span::symbol::sym;
1818
/// one we're expanding from.
1919
pub(crate) fn expand_panic<'cx>(
2020
cx: &'cx mut ExtCtxt<'_>,
21+
is_in_const_env: bool,
2122
sp: Span,
2223
tts: TokenStream,
2324
) -> MacroExpanderResult<'cx> {
2425
let mac = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 };
25-
expand(mac, cx, sp, tts)
26+
expand(is_in_const_env, mac, cx, sp, tts)
2627
}
2728

2829
/// This expands to either
@@ -31,14 +32,16 @@ pub(crate) fn expand_panic<'cx>(
3132
/// depending on the edition.
3233
pub(crate) fn expand_unreachable<'cx>(
3334
cx: &'cx mut ExtCtxt<'_>,
35+
is_in_const_env: bool,
3436
sp: Span,
3537
tts: TokenStream,
3638
) -> MacroExpanderResult<'cx> {
3739
let mac = if use_panic_2021(sp) { sym::unreachable_2021 } else { sym::unreachable_2015 };
38-
expand(mac, cx, sp, tts)
40+
expand(is_in_const_env, mac, cx, sp, tts)
3941
}
4042

4143
fn expand<'cx>(
44+
is_in_const_env: bool,
4245
mac: rustc_span::Symbol,
4346
cx: &'cx ExtCtxt<'_>,
4447
sp: Span,
@@ -50,6 +53,7 @@ fn expand<'cx>(
5053
cx.expr(
5154
sp,
5255
ExprKind::MacCall(P(MacCall {
56+
is_in_const_env,
5357
path: Path {
5458
span: sp,
5559
segments: cx

compiler/rustc_builtin_macros/src/env.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fn lookup_env<'cx>(cx: &'cx ExtCtxt<'_>, var: Symbol) -> Result<Symbol, VarError
2929

3030
pub(crate) fn expand_option_env<'cx>(
3131
cx: &'cx mut ExtCtxt<'_>,
32+
_is_in_const_env: bool,
3233
sp: Span,
3334
tts: TokenStream,
3435
) -> MacroExpanderResult<'cx> {
@@ -89,6 +90,7 @@ pub(crate) fn expand_option_env<'cx>(
8990

9091
pub(crate) fn expand_env<'cx>(
9192
cx: &'cx mut ExtCtxt<'_>,
93+
_is_in_const_env: bool,
9294
sp: Span,
9395
tts: TokenStream,
9496
) -> MacroExpanderResult<'cx> {

compiler/rustc_builtin_macros/src/format.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ fn expand_format_args_impl<'cx>(
10151015

10161016
pub(crate) fn expand_format_args<'cx>(
10171017
ecx: &'cx mut ExtCtxt<'_>,
1018+
_is_in_const_env: bool,
10181019
sp: Span,
10191020
tts: TokenStream,
10201021
) -> MacroExpanderResult<'cx> {
@@ -1023,6 +1024,7 @@ pub(crate) fn expand_format_args<'cx>(
10231024

10241025
pub(crate) fn expand_format_args_nl<'cx>(
10251026
ecx: &'cx mut ExtCtxt<'_>,
1027+
_is_in_const_env: bool,
10261028
sp: Span,
10271029
tts: TokenStream,
10281030
) -> MacroExpanderResult<'cx> {

compiler/rustc_builtin_macros/src/log_syntax.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult
44

55
pub(crate) fn expand_log_syntax<'cx>(
66
_cx: &'cx mut ExtCtxt<'_>,
7+
_is_in_const_env: bool,
78
sp: rustc_span::Span,
89
tts: TokenStream,
910
) -> MacroExpanderResult<'cx> {

compiler/rustc_builtin_macros/src/pattern_type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_span::{Span, sym};
77

88
pub(crate) fn expand<'cx>(
99
cx: &'cx mut ExtCtxt<'_>,
10+
_is_in_const_env: bool,
1011
sp: Span,
1112
tts: TokenStream,
1213
) -> MacroExpanderResult<'cx> {

compiler/rustc_builtin_macros/src/source_util.rs

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::util::{
3232
/// line!(): expands to the current line number
3333
pub(crate) fn expand_line(
3434
cx: &mut ExtCtxt<'_>,
35+
_is_in_const_env: bool,
3536
sp: Span,
3637
tts: TokenStream,
3738
) -> MacroExpanderResult<'static> {
@@ -47,6 +48,7 @@ pub(crate) fn expand_line(
4748
/* column!(): expands to the current column number */
4849
pub(crate) fn expand_column(
4950
cx: &mut ExtCtxt<'_>,
51+
_is_in_const_env: bool,
5052
sp: Span,
5153
tts: TokenStream,
5254
) -> MacroExpanderResult<'static> {
@@ -64,6 +66,7 @@ pub(crate) fn expand_column(
6466
/// out if we wanted.
6567
pub(crate) fn expand_file(
6668
cx: &mut ExtCtxt<'_>,
69+
_is_in_const_env: bool,
6770
sp: Span,
6871
tts: TokenStream,
6972
) -> MacroExpanderResult<'static> {
@@ -85,6 +88,7 @@ pub(crate) fn expand_file(
8588

8689
pub(crate) fn expand_stringify(
8790
cx: &mut ExtCtxt<'_>,
91+
_is_in_const_env: bool,
8892
sp: Span,
8993
tts: TokenStream,
9094
) -> MacroExpanderResult<'static> {
@@ -95,6 +99,7 @@ pub(crate) fn expand_stringify(
9599

96100
pub(crate) fn expand_mod(
97101
cx: &mut ExtCtxt<'_>,
102+
_is_in_const_env: bool,
98103
sp: Span,
99104
tts: TokenStream,
100105
) -> MacroExpanderResult<'static> {
@@ -111,6 +116,7 @@ pub(crate) fn expand_mod(
111116
/// unhygienically.
112117
pub(crate) fn expand_include<'cx>(
113118
cx: &'cx mut ExtCtxt<'_>,
119+
_is_in_const_env: bool,
114120
sp: Span,
115121
tts: TokenStream,
116122
) -> MacroExpanderResult<'cx> {
@@ -192,6 +198,7 @@ pub(crate) fn expand_include<'cx>(
192198
/// `include_str!`: read the given file, insert it as a literal string expr
193199
pub(crate) fn expand_include_str(
194200
cx: &mut ExtCtxt<'_>,
201+
_is_in_const_env: bool,
195202
sp: Span,
196203
tts: TokenStream,
197204
) -> MacroExpanderResult<'static> {
@@ -221,6 +228,7 @@ pub(crate) fn expand_include_str(
221228

222229
pub(crate) fn expand_include_bytes(
223230
cx: &mut ExtCtxt<'_>,
231+
_is_in_const_env: bool,
224232
sp: Span,
225233
tts: TokenStream,
226234
) -> MacroExpanderResult<'static> {

compiler/rustc_builtin_macros/src/trace_macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::errors;
77

88
pub(crate) fn expand_trace_macros(
99
cx: &mut ExtCtxt<'_>,
10+
_is_in_const_env: bool,
1011
sp: Span,
1112
tt: TokenStream,
1213
) -> MacroExpanderResult<'static> {

compiler/rustc_expand/src/base.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ pub trait BangProcMacro {
280280
fn expand<'cx>(
281281
&self,
282282
ecx: &'cx mut ExtCtxt<'_>,
283+
is_in_const_env: bool,
283284
span: Span,
284285
ts: TokenStream,
285286
) -> Result<TokenStream, ErrorGuaranteed>;
@@ -292,6 +293,7 @@ where
292293
fn expand<'cx>(
293294
&self,
294295
_ecx: &'cx mut ExtCtxt<'_>,
296+
_is_in_const_env: bool,
295297
_span: Span,
296298
ts: TokenStream,
297299
) -> Result<TokenStream, ErrorGuaranteed> {
@@ -331,6 +333,7 @@ pub trait TTMacroExpander {
331333
fn expand<'cx>(
332334
&self,
333335
ecx: &'cx mut ExtCtxt<'_>,
336+
is_in_const_env: bool,
334337
span: Span,
335338
input: TokenStream,
336339
) -> MacroExpanderResult<'cx>;
@@ -339,19 +342,20 @@ pub trait TTMacroExpander {
339342
pub type MacroExpanderResult<'cx> = ExpandResult<Box<dyn MacResult + 'cx>, ()>;
340343

341344
pub type MacroExpanderFn =
342-
for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>;
345+
for<'cx> fn(&'cx mut ExtCtxt<'_>, bool, Span, TokenStream) -> MacroExpanderResult<'cx>;
343346

344347
impl<F> TTMacroExpander for F
345348
where
346-
F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>,
349+
F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, bool, Span, TokenStream) -> MacroExpanderResult<'cx>,
347350
{
348351
fn expand<'cx>(
349352
&self,
350353
ecx: &'cx mut ExtCtxt<'_>,
354+
is_in_const_env: bool,
351355
span: Span,
352356
input: TokenStream,
353357
) -> MacroExpanderResult<'cx> {
354-
self(ecx, span, input)
358+
self(ecx, is_in_const_env, span, input)
355359
}
356360
}
357361

@@ -901,6 +905,7 @@ impl SyntaxExtension {
901905
pub fn dummy_bang(edition: Edition) -> SyntaxExtension {
902906
fn expander<'cx>(
903907
cx: &'cx mut ExtCtxt<'_>,
908+
_is_in_const_env: bool,
904909
span: Span,
905910
_: TokenStream,
906911
) -> MacroExpanderResult<'cx> {

compiler/rustc_expand/src/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ impl<'a> ExtCtxt<'a> {
5353

5454
pub fn macro_call(
5555
&self,
56+
is_in_const_env: bool,
5657
span: Span,
5758
path: ast::Path,
5859
delim: ast::token::Delimiter,
5960
tokens: ast::tokenstream::TokenStream,
6061
) -> P<ast::MacCall> {
6162
P(ast::MacCall {
63+
is_in_const_env,
6264
path,
6365
args: P(ast::DelimArgs {
6466
dspan: ast::tokenstream::DelimSpan { open: span, close: span },
@@ -435,6 +437,7 @@ impl<'a> ExtCtxt<'a> {
435437
self.expr_macro_call(
436438
span,
437439
self.macro_call(
440+
false,
438441
span,
439442
self.path_global(
440443
span,

0 commit comments

Comments
 (0)