Skip to content
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

ICE with macro defining invalid enum that derives a trait #96818

Closed
cdmistman opened this issue May 7, 2022 · 3 comments · Fixed by #100250
Closed

ICE with macro defining invalid enum that derives a trait #96818

cdmistman opened this issue May 7, 2022 · 3 comments · Fixed by #100250
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cdmistman
Copy link

Code

macro_rules! values {
	($($token:ident($value:literal) $(as $inner:ty)? => $attr:meta,)*) => {
		#[derive(Debug)]
		pub enum TokenKind {
			$(
				#[$attr]
				$token $($inner)? = $value,
			)*
		}
	};
}

values!(STRING(1) as (String) => cfg(test),);

Meta

rustc --version --verbose:

rustc 1.62.0-nightly (4dd8b420c 2022-05-01)
binary: rustc
commit-hash: 4dd8b420c027001e47b0d811a7e55e2fe1de1395
commit-date: 2022-05-01
host: aarch64-apple-darwin
release: 1.62.0-nightly
LLVM version: 14.0.1

Error output

$ cargo check
    Checking repro v0.1.0 (/blah/crates/repro)
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `(String)`
  --> crates/repro/src/lib.rs:7:14
   |
7  |                 $token $($inner)? = $value,
   |                          ^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}`
...
13 | tokens!(STRING(1) as (String) => cfg(test),);
   | -------------------------------------------- in this macro invocation
   |
   = note: this error originates in the macro `tokens` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores token `(String)` and any following
  --> crates/repro/src/lib.rs:7:14
   |
7  |                 $token $($inner)? = $value,
   |                          ^^^^^^
...
13 | tokens!(STRING(1) as (String) => cfg(test),);
   | -------------------------------------------- caused by the macro expansion here
   |
   = note: the usage of `tokens!` is likely invalid in item context

thread 'rustc' panicked at 'Stack should be empty: final_buf=FrameData { open_delim_sp: Some((Brace, crates/repro/src/lib.rs:4:22: 9:4 (#4))), inner: [(Token(Token { kind: Pound, span: crates/repro/src/lib.rs:6:5: 6:6 (#4) }), Alone), (Delimited(DelimSpan { open: crates/repro/src/lib.rs:6:6: 6:7 (#4), close: crates/repro/src/lib.rs:6:12: 6:13 (#4) }, Bracket, AttrAnnotatedTokenStream([(Token(Token { kind: Interpolated(NtMeta(..)), span: crates/repro/src/lib.rs:6:7: 6:12 (#4) }), Alone)])), Alone), (Token(Token { kind: Interpolated(NtIdent(..)), span: crates/repro/src/lib.rs:7:5: 7:11 (#4) }), Alone)] } stack=[FrameData { open_delim_sp: None, inner: [(Token(Token { kind: Ident("pub", false), span: crates/repro/src/lib.rs:4:3: 4:6 (#4) }), Alone), (Token(Token { kind: Ident("enum", false), span: crates/repro/src/lib.rs:4:7: 4:11 (#4) }), Alone), (Token(Token { kind: Ident("TokenKind", false), span: crates/repro/src/lib.rs:4:12: 4:21 (#4) }), Alone)] }]', compiler/rustc_parse/src/parser/attr_wrapper.rs:500:5
stack backtrace:
   0:        0x1031cbe94 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h23e3041a952a3192
   1:        0x10321d790 - core::fmt::write::hf7527de7ada02621
   2:        0x1031beeb0 - std::io::Write::write_fmt::h11ac03f695ea0bfd
   3:        0x1031ceb94 - std::panicking::default_hook::{{closure}}::hbc68a212d51bb096
   4:        0x1031ce870 - std::panicking::default_hook::h8ba60dc6deb30472
   5:        0x10a4c95e4 - rustc_driver[be97d6212fff1111]::DEFAULT_HOOK::{closure#0}::{closure#0}
   6:        0x1031cf270 - std::panicking::rust_panic_with_hook::h9b549e34f7164858
   7:        0x1031cf0c8 - std::panicking::begin_panic_handler::{{closure}}::h50e8103cafb0a3d6
   8:        0x1031cc370 - std::sys_common::backtrace::__rust_end_short_backtrace::h7046937afbaad16d
   9:        0x1031cee20 - _rust_begin_unwind
  10:        0x10324bc88 - core::panicking::panic_fmt::h4ae2eb332ac288b3
  11:        0x10db35250 - <rustc_parse[6f970b7ceb167d01]::parser::attr_wrapper::LazyTokenStreamImpl as rustc_ast[5b6630b34daf7a2]::tokenstream::CreateTokenStream>::create_token_stream
  12:        0x10dadd228 - rustc_parse[6f970b7ceb167d01]::prepend_attrs
  13:        0x10dadc900 - rustc_parse[6f970b7ceb167d01]::nt_to_tokenstream
  14:        0x10c88fcc0 - rustc_builtin_macros[ce373b3deb6b848d]::cfg_eval::cfg_eval
  15:        0x10c885804 - <rustc_builtin_macros[ce373b3deb6b848d]::derive::Expander as rustc_expand[3645db8203ef6e1d]::base::MultiItemModifier>::expand::{closure#0}
  16:        0x10cb9f3e8 - <rustc_resolve[82457f197e93ce33]::Resolver as rustc_expand[3645db8203ef6e1d]::base::ResolverExpand>::resolve_derives
  17:        0x10c8854b4 - <rustc_builtin_macros[ce373b3deb6b848d]::derive::Expander as rustc_expand[3645db8203ef6e1d]::base::MultiItemModifier>::expand
  18:        0x10d993e2c - <rustc_expand[3645db8203ef6e1d]::expand::MacroExpander>::fully_expand_fragment
  19:        0x10d991e04 - <rustc_expand[3645db8203ef6e1d]::expand::MacroExpander>::expand_crate
  20:        0x10a5896e0 - <rustc_session[ae993a5043b66882]::session::Session>::time::<core[e4699273b2677591]::result::Result<rustc_ast[5b6630b34daf7a2]::ast::Crate, rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>, rustc_interface[4ab8c5333806be77]::passes::configure_and_expand::{closure#1}>
  21:        0x10a584640 - rustc_interface[4ab8c5333806be77]::passes::configure_and_expand
  22:        0x10a605040 - <rustc_interface[4ab8c5333806be77]::queries::Queries>::expansion
  23:        0x10a475f48 - <rustc_interface[4ab8c5333806be77]::interface::Compiler>::enter::<rustc_driver[be97d6212fff1111]::run_compiler::{closure#1}::{closure#2}, core[e4699273b2677591]::result::Result<core[e4699273b2677591]::option::Option<rustc_interface[4ab8c5333806be77]::queries::Linker>, rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>>
  24:        0x10a4ce578 - rustc_span[595034ce2e7fee82]::with_source_map::<core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>, rustc_interface[4ab8c5333806be77]::interface::create_compiler_and_run<core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>, rustc_driver[be97d6212fff1111]::run_compiler::{closure#1}>::{closure#1}>
  25:        0x10a476634 - rustc_interface[4ab8c5333806be77]::interface::create_compiler_and_run::<core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>, rustc_driver[be97d6212fff1111]::run_compiler::{closure#1}>
  26:        0x10a470b6c - <scoped_tls[8a4b2aa1592938bb]::ScopedKey<rustc_span[595034ce2e7fee82]::SessionGlobals>>::set::<rustc_interface[4ab8c5333806be77]::interface::run_compiler<core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>, rustc_driver[be97d6212fff1111]::run_compiler::{closure#1}>::{closure#0}, core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>>
  27:        0x10a4bc8b4 - std[6fb83afb07978576]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[4ab8c5333806be77]::util::run_in_thread_pool_with_globals<rustc_interface[4ab8c5333806be77]::interface::run_compiler<core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>, rustc_driver[be97d6212fff1111]::run_compiler::{closure#1}>::{closure#0}, core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>>::{closure#0}, core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>>
  28:        0x10a4961d0 - <<std[6fb83afb07978576]::thread::Builder>::spawn_unchecked_<rustc_interface[4ab8c5333806be77]::util::run_in_thread_pool_with_globals<rustc_interface[4ab8c5333806be77]::interface::run_compiler<core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>, rustc_driver[be97d6212fff1111]::run_compiler::{closure#1}>::{closure#0}, core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>>::{closure#0}, core[e4699273b2677591]::result::Result<(), rustc_errors[6fdaf2daf3d0fd37]::ErrorGuaranteed>>::{closure#1} as core[e4699273b2677591]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  29:        0x1031d7b40 - std::sys::unix::thread::Thread::new::thread_start::h4b803e136338a26a
  30:        0x19718226c - __pthread_deallocate

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.62.0-nightly (4dd8b420c 2022-05-01) running on aarch64-apple-darwin

note: compiler flags: --crate-type lib -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C incremental

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `repro` due to 2 previous errors
Backtrace

$ RUST_BACKTRACE=1 cargo check
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <rustc_parse::parser::attr_wrapper::LazyTokenStreamImpl as rustc_ast::tokenstream::CreateTokenStream>::create_token_stream
   3: rustc_parse::prepend_attrs
   4: rustc_parse::nt_to_tokenstream
   5: rustc_builtin_macros::cfg_eval::cfg_eval
   6: <rustc_builtin_macros::derive::Expander as rustc_expand::base::MultiItemModifier>::expand::{closure#0}
   7: <rustc_resolve::Resolver as rustc_expand::base::ResolverExpand>::resolve_derives
   8: <rustc_builtin_macros::derive::Expander as rustc_expand::base::MultiItemModifier>::expand
   9: <rustc_expand::expand::MacroExpander>::fully_expand_fragment
  10: <rustc_expand::expand::MacroExpander>::expand_crate
  11: <rustc_session::session::Session>::time::<core::result::Result<rustc_ast::ast::Crate, rustc_errors::ErrorGuaranteed>, rustc_interface::passes::configure_and_expand::{closure#1}>
  12: rustc_interface::passes::configure_and_expand
  13: <rustc_interface::queries::Queries>::expansion
  14: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorGuaranteed>>
  15: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
  16: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>
  17: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@cdmistman cdmistman added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 7, 2022
@hellow554
Copy link
Contributor

Probably a duplicate of #81920

@hellow554
Copy link
Contributor

hellow554 commented May 9, 2022

Quick question: is this a real world code or just messing around? Because the other one was found using a fuzzer which produces some code and is typically not a high priority.
If your code is from some code you wrote, the priority might be increased.

@cdmistman
Copy link
Author

It's for a single-use DSL macro I defined in a prototype tokenizer I'm working on. Since type-defining DSLs are generally considered an anti-pattern, I'll point out syn seems to do the same

@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label May 12, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Sep 20, 2022
…aron1011

Manually cleanup token stream when macro expansion aborts.

In case of syntax error in macro expansion, the expansion code can decide to stop processing anything. In that case, the token stream is malformed. This makes downstream users, like derive macros, ICE.

In this case, this PR manually cleans up the token stream by closing all currently open delimiters.

Fixes rust-lang#96818.
Fixes rust-lang#80447.
Fixes rust-lang#81920.
Fixes rust-lang#91023.
@bors bors closed this as completed in 14b27cf Sep 20, 2022
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 20, 2024
Manually cleanup token stream when macro expansion aborts.

In case of syntax error in macro expansion, the expansion code can decide to stop processing anything. In that case, the token stream is malformed. This makes downstream users, like derive macros, ICE.

In this case, this PR manually cleans up the token stream by closing all currently open delimiters.

Fixes rust-lang/rust#96818.
Fixes rust-lang/rust#80447.
Fixes rust-lang/rust#81920.
Fixes rust-lang/rust#91023.
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Manually cleanup token stream when macro expansion aborts.

In case of syntax error in macro expansion, the expansion code can decide to stop processing anything. In that case, the token stream is malformed. This makes downstream users, like derive macros, ICE.

In this case, this PR manually cleans up the token stream by closing all currently open delimiters.

Fixes rust-lang/rust#96818.
Fixes rust-lang/rust#80447.
Fixes rust-lang/rust#81920.
Fixes rust-lang/rust#91023.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants