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

Commit 01b8e1b

Browse files
committed
[generic_assert] Avoid constant environments
1 parent 1555074 commit 01b8e1b

33 files changed

+152
-30
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ pub enum ClosureBinder {
17151715
/// is being invoked, and the `args` are arguments passed to it.
17161716
#[derive(Clone, Encodable, Decodable, Debug)]
17171717
pub struct MacCall {
1718+
pub is_in_const_env: bool,
17181719
pub path: Path,
17191720
pub args: P<DelimArgs>,
17201721
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ fn walk_attribute<T: MutVisitor>(vis: &mut T, attr: &mut Attribute) {
714714
}
715715

716716
fn walk_mac<T: MutVisitor>(vis: &mut T, mac: &mut MacCall) {
717-
let MacCall { path, args } = mac;
717+
let MacCall { is_in_const_env: _, path, args } = mac;
718718
vis.visit_path(path);
719719
visit_delim_args(vis, args);
720720
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V:
10021002
}
10031003

10041004
pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) -> V::Result {
1005-
let MacCall { path, args: _ } = mac;
1005+
let MacCall { is_in_const_env: _, path, args: _ } = mac;
10061006
visitor.visit_path(path, DUMMY_NODE_ID)
10071007
}
10081008

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)