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

Rustc crashes when parsing an ambiguous cast and compare #42954

Closed
kstrafe opened this issue Jun 28, 2017 · 5 comments
Closed

Rustc crashes when parsing an ambiguous cast and compare #42954

kstrafe opened this issue Jun 28, 2017 · 5 comments
Assignees
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.

Comments

@kstrafe
Copy link

kstrafe commented Jun 28, 2017

An unexpected compiler panic occurs when parsing a macro that casts without parentheses surrounding the cast.

I tried this code:

macro_rules! is_plainly_printable {
	($i:ident) => {
		$i as u32 < 0
	};
}

fn main() {
	let c = 'a';
	is_plainly_printable!(c);
}

I expected to see this happen:

error: `<` is interpreted as a start of generic arguments for `u32`, not a comparison
 --> src/main.rs:9:13
  |
9 | 	c as u32 < 0;
  | 	         - ^ interpreted as generic argument
  | 	         |
  | 	         not interpreted as comparison
  |
help: if you want to compare the casted value then write:
  | 	(c as u32) < 0;

Instead, this happened: the compiler panicked

Compiling compbug v0.1.0 (file:///home/kefin/stuf/docs/compbug)
Running `rustc --crate-name compbug src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fa650156f9d48338 -C extra-filename=-fa650156f9d48338 --out-dir /home/kefin/stuf/docs/compbug/target/debug/deps -L dependency=/home/kefin/stuf/docs/compbug/target/debug/deps`
error: `<` is interpreted as a start of generic arguments for `u32`, not a comparison
 --> src/main.rs:3:15
  |
3 | 		$i as u32 < 0
  | 		          - ^ interpreted as generic argument
  | 		          |
  | 		          not interpreted as comparison
  |
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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.20.0-nightly (f590a44ce 2017-06-27) running on x86_64-unknown-linux-gnu

thread 'rustc' panicked at 'called `Result::unwrap()` on an `Err` value: IllFormedSpan(Span { lo: BytePos(125), hi: BytePos(64), ctxt: #0 })', /checkout/src/libcore/result.rs:860
note: Run with `RUST_BACKTRACE=1` for a backtrace.

error: Could not compile `compbug`.

Caused by:
  process didn't exit successfully: `rustc --crate-name compbug src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fa650156f9d48338 -C extra-filename=-fa650156f9d48338 --out-dir /home/kefin/stuf/docs/compbug/target/debug/deps -L dependency=/home/kefin/stuf/docs/compbug/target/debug/deps` (exit code: 101)

Meta

rustc --version --verbose:

rustc 1.20.0-nightly (f590a44ce 2017-06-27)
binary: rustc
commit-hash: f590a44ce61888c78b9044817d8b798db5cd2ffd
commit-date: 2017-06-27
host: x86_64-unknown-linux-gnu
release: 1.20.0-nightly
LLVM version: 4.0

Backtrace:

   Compiling compbug v0.1.0 (file:///home/kefin/stuf/docs/compbug)
     Running `rustc --crate-name compbug src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fa650156f9d48338 -C extra-filename=-fa650156f9d48338 --out-dir /home/kefin/stuf/docs/compbug/target/debug/deps -L dependency=/home/kefin/stuf/docs/compbug/target/debug/deps`
error: `<` is interpreted as a start of generic arguments for `u32`, not a comparison
 --> src/main.rs:3:15
  |
3 | 		$i as u32 < 0
  | 		          - ^ interpreted as generic argument
  | 		          |
  | 		          not interpreted as comparison
  |
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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.20.0-nightly (f590a44ce 2017-06-27) running on x86_64-unknown-linux-gnu

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'called `Result::unwrap()` on an `Err` value: IllFormedSpan(Span { lo: BytePos(125), hi: BytePos(64), ctxt: #0 })', /checkout/src/libcore/result.rs:860
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:355
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:365
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:549
   5: std::panicking::begin_panic
             at /checkout/src/libstd/panicking.rs:511
   6: std::panicking::begin_panic_fmt
             at /checkout/src/libstd/panicking.rs:495
   7: rust_begin_unwind
             at /checkout/src/libstd/panicking.rs:471
   8: core::panicking::panic_fmt
             at /checkout/src/libcore/panicking.rs:71
   9: core::result::unwrap_failed
  10: <rustc_errors::emitter::EmitterWriter as rustc_errors::emitter::Emitter>::emit
  11: rustc_errors::diagnostic_builder::DiagnosticBuilder::emit
  12: syntax::parse::parser::Parser::parse_assoc_op_as
  13: syntax::parse::parser::Parser::parse_assoc_expr_with
  14: syntax::parse::parser::Parser::parse_stmt_without_recovery::{{closure}}
  15: syntax::parse::parser::Parser::parse_stmt_without_recovery
  16: syntax::parse::parser::Parser::parse_stmt_
  17: syntax::parse::parser::Parser::parse_full_stmt
  18: syntax::ext::expand::<impl syntax::parse::parser::Parser<'a>>::parse_expansion
  19: syntax::ext::tt::macro_rules::ParserAnyMacro::make
  20: syntax::ext::expand::<impl syntax::ext::base::MacResult for syntax::ext::tt::macro_rules::ParserAnyMacro<'a>>::make_stmts
  21: syntax::ext::expand::ExpansionKind::make_from
  22: syntax::ext::expand::MacroExpander::expand_bang_invoc
  23: syntax::ext::expand::MacroExpander::expand
  24: syntax::ext::expand::MacroExpander::expand_crate
  25: rustc_driver::driver::phase_2_configure_and_expand::{{closure}}
  26: rustc_driver::driver::phase_2_configure_and_expand
  27: rustc_driver::driver::compile_input
  28: rustc_driver::run_compiler

error: Could not compile `compbug`.

Caused by:
  process didn't exit successfully: `rustc --crate-name compbug src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fa650156f9d48338 -C extra-filename=-fa650156f9d48338 --out-dir /home/kefin/stuf/docs/compbug/target/debug/deps -L dependency=/home/kefin/stuf/docs/compbug/target/debug/deps` (exit code: 101)
@est31
Copy link
Member

est31 commented Jun 29, 2017

Using bisect-rust I've bisected it to 44eeb21, PR #42578 "Learn to parse a as usize < b".

cc @estebank

@arielb1
Copy link
Contributor

arielb1 commented Jun 29, 2017

cc @petrochenkov - you know how to get the right span

@estebank
Copy link
Contributor

The suggestion_span needs to be sourced in a different way. I'll defer to @petrochenkov if he has the bandwidth for this.

@arielb1 arielb1 added the regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. label Jun 29, 2017
@petrochenkov
Copy link
Contributor

This is not exactly an issue with a as usize < b recovery, but with span assignments for binary operator expressions. Note how ICE disappears if $i:ident is replaced with $i:expr.

To avoid weird spans, a span for $interpolated BINOP rhs_expr is adjusted - span of $interpolated is used instead of span of what $interpolated refers to. The problem is that for idents we can't distinguish between interpolated and not-interpolated cases by checking self.prev_token_kind == PrevTokenKind::Interpolated because interpolated idents are transient and are immediately unwrapped into not-interpolated idents.
So this ICE should manifest in any diagnostics using a span of $interpolated_ident BINOP rhs_expr.

I think there's a simple way to detect interpolated idents here, I'll submit a fix tomorrow.

@petrochenkov petrochenkov self-assigned this Jul 3, 2017
@sfackler sfackler added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Jul 3, 2017
@petrochenkov
Copy link
Contributor

Fixed in #43060

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.
Projects
None yet
Development

No branches or pull requests

6 participants