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 failed to unescape char literal #120397

Closed
matthiaskrgr opened this issue Jan 26, 2024 · 2 comments · Fixed by #120460
Closed

ICE failed to unescape char literal #120397

matthiaskrgr opened this issue Jan 26, 2024 · 2 comments · Fixed by #120460
Assignees
Labels
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.

Comments

@matthiaskrgr
Copy link
Member

snippet:

fn a() {
   'ab
} 

Version information

rustc 1.77.0-nightly (e7bbe8ce9 2024-01-26)
binary: rustc
commit-hash: e7bbe8ce933123a8e327c79e38bcb790595e6a65
commit-date: 2024-01-26
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc

Program output

error: expected `while`, `for`, `loop` or `{` after a label
 --> /tmp/icemaker_global_tempdir.awRLrWEr9xBk/rustc_testrunner_tmpdir_reporting.AYMxBOFwCLUT/mvce.rs:3:1
  |
3 | } 
  | ^ expected `while`, `for`, `loop` or `{` after a label
  |
help: add `'` to close the char literal
  |
2 |    'ab'
  |       +

thread 'rustc' panicked at compiler/rustc_ast/src/util/literal.rs:66:34:
failed to unescape char literal
stack backtrace:
   0:     0x7f6d4e98bf86 - std::backtrace_rs::backtrace::libunwind::trace::h514ead2753e1782f
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f6d4e98bf86 - std::backtrace_rs::backtrace::trace_unsynchronized::hbc462e8372651887
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f6d4e98bf86 - std::sys_common::backtrace::_print_fmt::he3092ef183bdd613
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f6d4e98bf86 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h908f80a389799910
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f6d4e9de880 - core::fmt::rt::Argument::fmt::ha9f3d8b02e25296c
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/core/src/fmt/rt.rs:142:9
   5:     0x7f6d4e9de880 - core::fmt::write::hef880bc86bd63d4b
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f6d4e97f94f - std::io::Write::write_fmt::h780869503f755b5a
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/io/mod.rs:1810:15
   7:     0x7f6d4e98bd64 - std::sys_common::backtrace::_print::h52cc78326ec17ac5
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f6d4e98bd64 - std::sys_common::backtrace::print::h44b4c057fd1c7c3f
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f6d4e98eaf7 - std::panicking::default_hook::{{closure}}::h99cbf699c09f8685
  10:     0x7f6d4e98e859 - std::panicking::default_hook::hbc89d54398681576
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/panicking.rs:292:9
  11:     0x7f6d5174acec - std[1d0ea5b5e602c844]::panicking::update_hook::<alloc[d84df3010862da1b]::boxed::Box<rustc_driver_impl[2078f68ece4ea2b8]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f6d4e98f246 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h99950b3f8a1ddee8
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/alloc/src/boxed.rs:2029:9
  13:     0x7f6d4e98f246 - std::panicking::rust_panic_with_hook::he160495ec53b11e6
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/panicking.rs:785:13
  14:     0x7f6d4e98ef59 - std::panicking::begin_panic_handler::{{closure}}::ha86f6320ad4cac49
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/panicking.rs:651:13
  15:     0x7f6d4e98c486 - std::sys_common::backtrace::__rust_end_short_backtrace::h19c3911246e5b7ea
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f6d4e98ece4 - rust_begin_unwind
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/panicking.rs:647:5
  17:     0x7f6d4e9daf85 - core::panicking::panic_fmt::hd869deda4756f0c1
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/core/src/panicking.rs:72:14
  18:     0x7f6d52b3f9cd - <rustc_ast[a2e9ff0ff4e44aed]::ast::LitKind>::from_token_lit
  19:     0x7f6d52c0b224 - <rustc_ast_lowering[10dbbff99856d352]::LoweringContext>::lower_expr_mut
  20:     0x7f6d53348ddf - <rustc_ast_lowering[10dbbff99856d352]::LoweringContext>::lower_block_noalloc
  21:     0x7f6d5334361f - <rustc_ast_lowering[10dbbff99856d352]::LoweringContext>::lower_maybe_coroutine_body
  22:     0x7f6d537c4e48 - <rustc_ast_lowering[10dbbff99856d352]::LoweringContext>::lower_item_kind
  23:     0x7f6d52da6151 - <rustc_ast_lowering[10dbbff99856d352]::item::ItemLowerer>::lower_node
  24:     0x7f6d537ad9bb - rustc_ast_lowering[10dbbff99856d352]::lower_to_hir
  25:     0x7f6d537ad124 - rustc_query_impl[210ae1d54e37601d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[210ae1d54e37601d]::query_impl::hir_crate::dynamic_query::{closure#2}::{closure#0}, rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 8usize]>>
  26:     0x7f6d537ab2f2 - rustc_query_system[2f3cbd302947754a]::query::plumbing::try_execute_query::<rustc_query_impl[210ae1d54e37601d]::DynamicConfig<rustc_query_system[2f3cbd302947754a]::query::caches::SingleCache<rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[210ae1d54e37601d]::plumbing::QueryCtxt, false>
  27:     0x7f6d537aae46 - rustc_query_impl[210ae1d54e37601d]::query_impl::hir_crate::get_query_non_incr::__rust_end_short_backtrace
  28:     0x7f6d52b8ab9f - rustc_query_impl[210ae1d54e37601d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[210ae1d54e37601d]::query_impl::hir_attrs::dynamic_query::{closure#2}::{closure#0}, rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 8usize]>>
  29:     0x7f6d52b8a127 - rustc_query_system[2f3cbd302947754a]::query::plumbing::try_execute_query::<rustc_query_impl[210ae1d54e37601d]::DynamicConfig<rustc_query_system[2f3cbd302947754a]::query::caches::VecCache<rustc_hir[95673764c296ef68]::hir_id::OwnerId, rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[210ae1d54e37601d]::plumbing::QueryCtxt, false>
  30:     0x7f6d52b89e8d - rustc_query_impl[210ae1d54e37601d]::query_impl::hir_attrs::get_query_non_incr::__rust_end_short_backtrace
  31:     0x7f6d52a58534 - <rustc_middle[89c572df7d816277]::hir::map::Map>::attrs
  32:     0x7f6d537a87c1 - rustc_passes[84bfad27b65c8e42]::entry::entry_fn
  33:     0x7f6d537a8766 - rustc_query_impl[210ae1d54e37601d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[210ae1d54e37601d]::query_impl::entry_fn::dynamic_query::{closure#2}::{closure#0}, rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 12usize]>>
  34:     0x7f6d537a8749 - <rustc_query_impl[210ae1d54e37601d]::query_impl::entry_fn::dynamic_query::{closure#2} as core[1eaf6a36b93e3ccb]::ops::function::FnOnce<(rustc_middle[89c572df7d816277]::ty::context::TyCtxt, ())>>::call_once
  35:     0x7f6d537a8049 - rustc_query_system[2f3cbd302947754a]::query::plumbing::try_execute_query::<rustc_query_impl[210ae1d54e37601d]::DynamicConfig<rustc_query_system[2f3cbd302947754a]::query::caches::SingleCache<rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 12usize]>>, false, false, false>, rustc_query_impl[210ae1d54e37601d]::plumbing::QueryCtxt, false>
  36:     0x7f6d537a7e13 - rustc_query_impl[210ae1d54e37601d]::query_impl::entry_fn::get_query_non_incr::__rust_end_short_backtrace
  37:     0x7f6d53353555 - rustc_interface[567ccd780f164b19]::passes::analysis
  38:     0x7f6d5335345f - rustc_query_impl[210ae1d54e37601d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[210ae1d54e37601d]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 1usize]>>
  39:     0x7f6d536c7bb2 - rustc_query_system[2f3cbd302947754a]::query::plumbing::try_execute_query::<rustc_query_impl[210ae1d54e37601d]::DynamicConfig<rustc_query_system[2f3cbd302947754a]::query::caches::SingleCache<rustc_middle[89c572df7d816277]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[210ae1d54e37601d]::plumbing::QueryCtxt, false>
  40:     0x7f6d536c7915 - rustc_query_impl[210ae1d54e37601d]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7f6d53741633 - rustc_interface[567ccd780f164b19]::interface::run_compiler::<core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>, rustc_driver_impl[2078f68ece4ea2b8]::run_compiler::{closure#0}>::{closure#0}
  42:     0x7f6d539cf306 - std[1d0ea5b5e602c844]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[567ccd780f164b19]::util::run_in_thread_with_globals<rustc_interface[567ccd780f164b19]::util::run_in_thread_pool_with_globals<rustc_interface[567ccd780f164b19]::interface::run_compiler<core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>, rustc_driver_impl[2078f68ece4ea2b8]::run_compiler::{closure#0}>::{closure#0}, core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>>::{closure#0}, core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>>
  43:     0x7f6d539cf133 - <<std[1d0ea5b5e602c844]::thread::Builder>::spawn_unchecked_<rustc_interface[567ccd780f164b19]::util::run_in_thread_with_globals<rustc_interface[567ccd780f164b19]::util::run_in_thread_pool_with_globals<rustc_interface[567ccd780f164b19]::interface::run_compiler<core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>, rustc_driver_impl[2078f68ece4ea2b8]::run_compiler::{closure#0}>::{closure#0}, core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>>::{closure#0}, core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1eaf6a36b93e3ccb]::result::Result<(), rustc_span[950276472281dfd0]::ErrorGuaranteed>>::{closure#1} as core[1eaf6a36b93e3ccb]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  44:     0x7f6d4e998835 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hba0b80fd74ce5417
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/alloc/src/boxed.rs:2015:9
  45:     0x7f6d4e998835 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h0ab2d213dcdff51c
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/alloc/src/boxed.rs:2015:9
  46:     0x7f6d4e998835 - std::sys::pal::unix::thread::Thread::new::thread_start::h7eeca5a8e186b7ad
                               at /rustc/e7bbe8ce933123a8e327c79e38bcb790595e6a65/library/std/src/sys/pal/unix/thread.rs:108:17
  47:     0x7f6d4e7849eb - <unknown>
  48:     0x7f6d4e8087cc - <unknown>
  49:                0x0 - <unknown>

error: 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.77.0-nightly (e7bbe8ce9 2024-01-26) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [hir_crate] getting the crate HIR
#1 [hir_attrs] getting HIR owner attributes in ``
#2 [entry_fn] looking up the entry function of a crate
#3 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error


@matthiaskrgr matthiaskrgr added 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. C-bug Category: This is a bug. labels Jan 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 26, 2024
@matthiaskrgr
Copy link
Member Author

The assertion was added in #120329 cc @nnethercote

@nnethercote nnethercote self-assigned this Jan 26, 2024
@nnethercote
Copy link
Contributor

It's Saturday here, I'll fix it on Monday.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 26, 2024
nnethercote added a commit to nnethercote/rust that referenced this issue Jan 29, 2024
Currently the parser will interpret any label in certain positions as a
mistyped char literal, on the assumption that the trailing single quote
was accidentally omitted. This is reasonable for a label like 'a (because
'a' would be valid) but not reasonable for a label like 'abc (because
'abc' is not valid).

This commit restricts this behaviour only to labels that would be valid
char literals, via the new `could_be_unclosed_char_literal` function.
The commit also augments the `label-is-actually-char.rs` test in a
couple of ways:
- Adds testing of labels with identifiers longer than one char, e.g.
  'abc.
- Adds a new match with simpler patterns, because the
  `recover_unclosed_char` call in `parse_pat_with_range_pat` was not
  being exercised (in this test or any other ui tests).

Fixes rust-lang#120397, an assertion failure, which was caused by this behaviour
in the parser interacting with some new stricter char literal checking
added in rust-lang#120329.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 30, 2024
…-errors

Be more careful about interpreting a label/lifetime as a mistyped char literal.

Currently the parser interprets any label/lifetime in certain positions as a mistyped char literal, on the assumption that the trailing single quote was accidentally omitted. In such cases it gives an error with a suggestion to add the trailing single quote, and then puts the appropriate char literal into the AST. This behaviour was introduced in rust-lang#101293.

This is reasonable for a case like this:
```
let c = 'a;
```
because `'a'` is a valid char literal. It's less reasonable for a case like this:
```
let c = 'abc;
```
because `'abc'` is not a valid char literal.

Prior to rust-lang#120329 this could result in some sub-optimal suggestions in error messages, but nothing else. But rust-lang#120329 changed `LitKind::from_token_lit` to assume that the char/byte/string literals it receives are valid, and to assert if not. This is reasonable because the lexer does not produce invalid char/byte/string literals in general. But in this "interpret label/lifetime as unclosed char literal" case the parser can produce an invalid char literal with contents such as `abc`, which triggers an assertion failure.

This PR changes the parser so it's more cautious about interpreting labels/lifetimes as unclosed char literals.

Fixes rust-lang#120397.

r? `@compiler-errors`
@bors bors closed this as completed in 306612e Jan 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 30, 2024
Rollup merge of rust-lang#120460 - nnethercote:fix-120397, r=compiler-errors

Be more careful about interpreting a label/lifetime as a mistyped char literal.

Currently the parser interprets any label/lifetime in certain positions as a mistyped char literal, on the assumption that the trailing single quote was accidentally omitted. In such cases it gives an error with a suggestion to add the trailing single quote, and then puts the appropriate char literal into the AST. This behaviour was introduced in rust-lang#101293.

This is reasonable for a case like this:
```
let c = 'a;
```
because `'a'` is a valid char literal. It's less reasonable for a case like this:
```
let c = 'abc;
```
because `'abc'` is not a valid char literal.

Prior to rust-lang#120329 this could result in some sub-optimal suggestions in error messages, but nothing else. But rust-lang#120329 changed `LitKind::from_token_lit` to assume that the char/byte/string literals it receives are valid, and to assert if not. This is reasonable because the lexer does not produce invalid char/byte/string literals in general. But in this "interpret label/lifetime as unclosed char literal" case the parser can produce an invalid char literal with contents such as `abc`, which triggers an assertion failure.

This PR changes the parser so it's more cautious about interpreting labels/lifetimes as unclosed char literals.

Fixes rust-lang#120397.

r? `@compiler-errors`
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. 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.

4 participants