-
Notifications
You must be signed in to change notification settings - Fork 13.4k
rustc_expand can be tricked into infinite loops #95698
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
Comments
A workaround is to disambiguate the two macro arms with parentheses and reorder the match arms: macro_rules! from_cow_impls {
($( ( $from: ty, $normalizer: expr ) ),+ $(,)? ) => {
$( impl<'a> From<$from> for LhsValue<'a> {
fn from(val: $from) -> Self {
LhsValue::Bytes($normalizer(val))
}
} )+
};
($( $from: ty ),+ $(,)? ) => {
from_cow_impls!(
$( ($from, Cow::from) ),+
);
};
}
from_cow_impls!(
&'a [u8],
Vec<u8>,
);
from_cow_impls!(
(&'a str, |x: &'a str| Cow::from(x.as_bytes())),
(String, |x: String| Cow::from(x.into_bytes())),
); |
The fix here is probably to add a rust/compiler/rustc_expand/src/expand.rs Line 624 in f262ca1
|
@rustbot label +E-mentor +E-easy |
@rustbot claim |
Today I tried to reproduce the problem on my machine and try to have simplify the minimal reproducible example. /// Code that cause the rust issue #95698
macro_rules! from_cow_impls {
($( $from: ty ),+ $(,)? ) => {
// recursion call
from_cow_impls!(
$( $from, Cow::from ),+
);
};
($( $from: ty, $normalizer: expr ),+ $(,)? ) => {
$( impl<'a> From<$from> for LhsValue<'a> {
fn from(val: $from) -> Self {
LhsValue::Bytes($normalizer(val))
}
} )+
};
}
from_cow_impls!(
&'a [u8], /*callback*/,
Vec<u8>, /*callback*/,
);
In particular, it looks like the recursion problem happens when we have the optional parameter null! |
looks like a parser error, this is the list of tokens that return the workaround TokenStream produced with workaround solution!
TokenStream produced with buggy code!
The Tokens continue to accumulate, and max deep is never reached. @rustbot label +T-compiler |
There doesn't seem to be a PR associated with this issue, @vincenzopalazzo @jyn514 can I claim this? |
There is some work on my side on this reported in the zulip chat and I have some half work around some branch. However, my solution it is not super clean, do you have the solution on how to implement it? if yes, so feel free to claim it and can you ping me when you open the PR? I'm super curious to read the solution code |
@rustbot label -E-easy This require to develop a new attribute for the rustc |
Uh oh!
There was an error while loading. Please reload this page.
I tried this code:
I expected to see this happen: Compilation succeeds or errors.
Instead, this happened: The compiler hangs indefinitely. GDB shows it stuck in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_expand/mbe/macro_parser/struct.TtParser.html#method.parse_tt.
Meta
rustc --version --verbose
:Backtrace
@rustbot label +A-macros
The text was updated successfully, but these errors were encountered: