Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3128760

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, as far as I can tell, not feasible 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 originated in the `rustc_parse` crate.
2 parents 5a6036a + b6501c5 commit 3128760

34 files changed

+152
-40
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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> {

0 commit comments

Comments
 (0)