-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Fix anon const def-creation when macros are involved take 2 #130337
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,61 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { | |
); | ||
assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation"); | ||
} | ||
|
||
/// Determines whether the const argument `AnonConst` is a simple macro call, optionally | ||
/// surrounded with braces. | ||
/// | ||
/// If this const argument *is* a trivial macro call then the id for the macro call is | ||
/// returned along with the information required to build the anon const's def if | ||
/// the macro call expands to a non-trivial expression. | ||
fn is_const_arg_trivial_macro_expansion( | ||
&self, | ||
anon_const: &'a AnonConst, | ||
) -> Option<(PendingAnonConstInfo, NodeId)> { | ||
let (block_was_stripped, expr) = anon_const.value.maybe_unwrap_block(); | ||
match expr { | ||
Expr { kind: ExprKind::MacCall(..), id, .. } => Some(( | ||
PendingAnonConstInfo { | ||
id: anon_const.id, | ||
span: anon_const.value.span, | ||
block_was_stripped, | ||
}, | ||
*id, | ||
)), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Determines whether the expression `const_arg_sub_expr` is a simple macro call, sometimes | ||
/// surrounded with braces if a set of braces has not already been entered. This is required | ||
/// as `{ N }` is treated as equivalent to a bare parameter `N` whereas `{{ N }}` is treated as | ||
/// a real block expression and is lowered to an anonymous constant which is not allowed to use | ||
/// generic parameters. | ||
/// | ||
/// If this expression is a trivial macro call then the id for the macro call is | ||
/// returned along with the information required to build the anon const's def if | ||
/// the macro call expands to a non-trivial expression. | ||
fn is_const_arg_sub_expr_trivial_macro_expansion( | ||
&self, | ||
const_arg_sub_expr: &'a Expr, | ||
) -> Option<(PendingAnonConstInfo, NodeId)> { | ||
let pending_anon = self.pending_anon_const_info.unwrap_or_else(|| | ||
panic!("Checking expr is trivial macro call without having entered anon const: `{const_arg_sub_expr:?}`"), | ||
); | ||
|
||
let (block_was_stripped, expr) = if pending_anon.block_was_stripped { | ||
(true, const_arg_sub_expr) | ||
} else { | ||
const_arg_sub_expr.maybe_unwrap_block() | ||
}; | ||
|
||
match expr { | ||
Expr { kind: ExprKind::MacCall(..), id, .. } => { | ||
Some((PendingAnonConstInfo { block_was_stripped, ..pending_anon }, *id)) | ||
} | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { | ||
|
@@ -354,12 +409,12 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { | |
// items will be messed up, but that's ok because there can't be any if we're just looking | ||
// for bare idents. | ||
|
||
if matches!(constant.value.maybe_unwrap_block().kind, ExprKind::MacCall(..)) { | ||
// See self.pending_anon_const_info for explanation | ||
self.pending_anon_const_info = | ||
Some(PendingAnonConstInfo { id: constant.id, span: constant.value.span }); | ||
return visit::walk_anon_const(self, constant); | ||
} else if constant.value.is_potential_trivial_const_arg() { | ||
if let Some((pending_anon, macro_invoc)) = | ||
self.is_const_arg_trivial_macro_expansion(constant) | ||
{ | ||
self.pending_anon_const_info = Some(pending_anon); | ||
return self.visit_macro_invoc(macro_invoc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change to call |
||
} else if constant.value.is_potential_trivial_const_arg(true) { | ||
return visit::walk_anon_const(self, constant); | ||
} | ||
|
||
|
@@ -368,23 +423,36 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { | |
} | ||
|
||
fn visit_expr(&mut self, expr: &'a Expr) { | ||
if matches!(expr.kind, ExprKind::MacCall(..)) { | ||
return self.visit_macro_invoc(expr.id); | ||
// If we're visiting the expression of a const argument that was a macro call then | ||
// check if it is *still* unknown whether it is a trivial const arg or not. If so | ||
// recurse into the macro call and delay creating the anon const def until expansion. | ||
if self.pending_anon_const_info.is_some() | ||
&& let Some((pending_anon, macro_invoc)) = | ||
self.is_const_arg_sub_expr_trivial_macro_expansion(expr) | ||
{ | ||
self.pending_anon_const_info = Some(pending_anon); | ||
return self.visit_macro_invoc(macro_invoc); | ||
} | ||
|
||
let grandparent_def = if let Some(pending_anon) = self.pending_anon_const_info.take() { | ||
// See self.pending_anon_const_info for explanation | ||
if !expr.is_potential_trivial_const_arg() { | ||
// See self.pending_anon_const_info for explanation | ||
let parent_def = self | ||
.pending_anon_const_info | ||
.take() | ||
// If we already stripped away a set of braces then do not do it again when determining | ||
// if the macro expanded to a trivial const arg. This arises in cases such as: | ||
// `Foo<{ bar!() }>` where `bar!()` expands to `{ N }`. This should not be considered a | ||
// trivial const argument even though `{ N }` by itself *is*. | ||
.filter(|pending_anon| { | ||
!expr.is_potential_trivial_const_arg(!pending_anon.block_was_stripped) | ||
}) | ||
.map(|pending_anon| { | ||
self.create_def(pending_anon.id, kw::Empty, DefKind::AnonConst, pending_anon.span) | ||
} else { | ||
self.parent_def | ||
} | ||
} else { | ||
self.parent_def | ||
}; | ||
}) | ||
.unwrap_or(self.parent_def); | ||
|
||
self.with_parent(grandparent_def, |this| { | ||
self.with_parent(parent_def, |this| { | ||
let parent_def = match expr.kind { | ||
ExprKind::MacCall(..) => return this.visit_macro_invoc(expr.id), | ||
ExprKind::Closure(..) | ExprKind::Gen(..) => { | ||
this.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
macro_rules! y { | ||
() => { | ||
N | ||
}; | ||
} | ||
|
||
struct A<const N: usize>; | ||
|
||
fn foo<const N: usize>() -> A<{ y!() }> { | ||
BoxyUwU marked this conversation as resolved.
Show resolved
Hide resolved
|
||
A::<1> | ||
//~^ ERROR: mismatched types | ||
} | ||
|
||
fn main() {} |
14 changes: 14 additions & 0 deletions
14
tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/trivial-const-arg-macro-braced-expansion.rs:10:5 | ||
| | ||
LL | fn foo<const N: usize>() -> A<{ y!() }> { | ||
| ----------- expected `A<N>` because of return type | ||
LL | A::<1> | ||
| ^^^^^^ expected `N`, found `1` | ||
| | ||
= note: expected struct `A<N>` | ||
found struct `A<1>` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
15 changes: 15 additions & 0 deletions
15
tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
macro_rules! y { | ||
() => { | ||
N | ||
//~^ ERROR: generic parameters may not be used in const operations | ||
}; | ||
} | ||
|
||
struct A<const N: usize>; | ||
|
||
#[rustfmt::skip] | ||
fn foo<const N: usize>() -> A<{{ y!() }}> { | ||
A::<1> | ||
} | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: generic parameters may not be used in const operations | ||
--> $DIR/trivial-const-arg-macro-nested-braces-2.rs:3:9 | ||
| | ||
LL | N | ||
| ^ cannot perform const operation using `N` | ||
... | ||
LL | fn foo<const N: usize>() -> A<{{ y!() }}> { | ||
| ---- in this macro invocation | ||
| | ||
= help: const parameters may only be used as standalone arguments, i.e. `N` | ||
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions | ||
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 1 previous error | ||
|
15 changes: 15 additions & 0 deletions
15
tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[rustfmt::skip] | ||
macro_rules! y { | ||
() => { | ||
{ N } | ||
//~^ ERROR: generic parameters may not be used in const operations | ||
}; | ||
} | ||
|
||
struct A<const N: usize>; | ||
|
||
fn foo<const N: usize>() -> A<{ y!() }> { | ||
A::<1> | ||
} | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: generic parameters may not be used in const operations | ||
--> $DIR/trivial-const-arg-macro-nested-braces.rs:4:11 | ||
| | ||
LL | { N } | ||
| ^ cannot perform const operation using `N` | ||
... | ||
LL | fn foo<const N: usize>() -> A<{ y!() }> { | ||
| ---- in this macro invocation | ||
| | ||
= help: const parameters may only be used as standalone arguments, i.e. `N` | ||
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions | ||
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 1 previous error | ||
|
9 changes: 9 additions & 0 deletions
9
tests/ui/const-generics/early/trivial-const-arg-nested-braces.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
struct A<const N: usize>; | ||
|
||
#[rustfmt::skip] | ||
fn foo<const N: usize>() -> A<{ { N } }> { | ||
//~^ ERROR: generic parameters may not be used in const operations | ||
A::<1> | ||
} | ||
|
||
fn main() {} |
11 changes: 11 additions & 0 deletions
11
tests/ui/const-generics/early/trivial-const-arg-nested-braces.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: generic parameters may not be used in const operations | ||
--> $DIR/trivial-const-arg-nested-braces.rs:4:35 | ||
| | ||
LL | fn foo<const N: usize>() -> A<{ { N } }> { | ||
| ^ cannot perform const operation using `N` | ||
| | ||
= help: const parameters may only be used as standalone arguments, i.e. `N` | ||
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions | ||
|
||
error: aborting due to 1 previous error | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if it would be clearer to return
Option<&Expr>
(also matching the function's name)? Then it could be used asexpr.maybe_unwrap_block().unwrap_or(expr)
e.g. Maybe that'd be worse, but it just came to mind.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think this is probably just worse for every caller unfortunately because this is not the nicest name/signature combo 😅