Skip to content

[generic_assert] Avoid constant environments #134126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DelimArgs>,
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ fn walk_attribute<T: MutVisitor>(vis: &mut T, attr: &mut Attribute) {
}

fn walk_mac<T: MutVisitor>(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);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ impl<'cx, 'a> Context<'cx, 'a> {
/// }
/// }
/// ```
pub(super) fn build(mut self, mut cond_expr: P<Expr>, panic_path: Path) -> P<Expr> {
pub(super) fn build(
mut self,
mut cond_expr: P<Expr>,
is_in_const_env: bool,
panic_path: Path,
) -> P<Expr> {
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;
Expand Down Expand Up @@ -147,7 +152,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
/// __capture0,
/// ...
/// );
fn build_panic(&self, expr_str: &str, panic_path: Path) -> P<Expr> {
fn build_panic(&self, expr_str: &str, is_in_const_env: bool, panic_path: Path) -> P<Expr> {
let escaped_expr_str = escape_to_fmt(expr_str);
let initial = [
TokenTree::token_joint(
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/concat_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/concat_idents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_builtin_macros/src/edition_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -50,6 +53,7 @@ fn expand<'cx>(
cx.expr(
sp,
ExprKind::MacCall(P(MacCall {
is_in_const_env,
path: Path {
span: sp,
segments: cx
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn lookup_env<'cx>(cx: &'cx ExtCtxt<'_>, var: Symbol) -> Result<Symbol, VarError

pub(crate) fn expand_option_env<'cx>(
cx: &'cx mut ExtCtxt<'_>,
_is_in_const_env: bool,
sp: Span,
tts: TokenStream,
) -> MacroExpanderResult<'cx> {
Expand Down Expand Up @@ -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> {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/log_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/pattern_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_builtin_macros/src/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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> {
Expand All @@ -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> {
Expand All @@ -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> {
Expand All @@ -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> {
Expand All @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/trace_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenStream, ErrorGuaranteed>;
Expand All @@ -292,6 +293,7 @@ where
fn expand<'cx>(
&self,
_ecx: &'cx mut ExtCtxt<'_>,
_is_in_const_env: bool,
_span: Span,
ts: TokenStream,
) -> Result<TokenStream, ErrorGuaranteed> {
Expand Down Expand Up @@ -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>;
Expand All @@ -339,19 +342,20 @@ pub trait TTMacroExpander {
pub type MacroExpanderResult<'cx> = ExpandResult<Box<dyn MacResult + 'cx>, ()>;

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<F> 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)
}
}

Expand Down Expand Up @@ -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> {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ast::MacCall> {
P(ast::MacCall {
is_in_const_env,
path,
args: P(ast::DelimArgs {
dspan: ast::tokenstream::DelimSpan { open: span, close: span },
Expand Down Expand Up @@ -435,6 +437,7 @@ impl<'a> ExtCtxt<'a> {
self.expr_macro_call(
span,
self.macro_call(
false,
span,
self.path_global(
span,
Expand Down
Loading
Loading