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: unexpected resolution for an identifier in pattern #135614

Closed
matthiaskrgr opened this issue Jan 17, 2025 · 4 comments · Fixed by #135663
Closed

ICE: unexpected resolution for an identifier in pattern #135614

matthiaskrgr opened this issue Jan 17, 2025 · 4 comments · Fixed by #135663
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

auto-reduced (treereduce-rust):

use Bar::foo;

trait Bar {
    fn foo(&self) {}
}

fn main() {
    let foo: Arc<dyn Foo> = propagate2(&j);
}
original code

original:

// Check that more complex receivers work:
// terminator of the MIR, which has had the PostAnalysisNormalize pass performed on it.
//@ error-pattern: Uninitialized value was created by an allocation

//@ revisions: cfi kcfi

//@ only-linux
//@ [cfi] needs-sanitizer-cfi
//@ [kcfi] needs-sanitizer-kcfi
//@ compile-flags: -C target-feature=-crt-static
//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi
//@ [cfi] compile-flags: -Z sanitizer=cfi
//@ [kcfi] compile-flags: -Z sanitizer=kcfi
//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off
//@ run-pass

use Bar::foo;

trait Foo {
    fn foo(self: Arc<Self>)(&self)
}

struct FooImpl;

impl Foo for C {
    fn rpit_fn(self: Iter<Self>) {}
}

trait Bar {
    fn foo(&self) {}
}

struct BarImpl;

impl Bar for BarImpl {
    type T = i32;
    fn bar(&self) -> Self::T { 7 }
}

fn main() {
    let foo: Arc<dyn Foo> = propagate2(&j);
    foo.as_ptr();

    let bar: &dyn Yprintln!("p1")T=i32> = &BarImpl
    assert_eq!(use_closure_once.bar(), 3);
}

Version information

rustc 1.86.0-nightly (bb3fa4b48 2025-01-17)
binary: rustc
commit-hash: bb3fa4b48eef8dc0815b0464e56f1fb82fe2cdfc
commit-date: 2025-01-17
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

Possibly related line of code:

self.r.dcx().span_delayed_bug(
ident.span,
"unexpected `SelfCtor` in pattern, expected identifier"
);
None
}
_ => span_bug!(
ident.span,
"unexpected resolution for an identifier in pattern: {:?}",
res,
),
}
}

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

Program output

error[E0658]: `use` associated items of traits is unstable
 --> /tmp/icemaker_global_tempdir.Z7Hkc1ETV8nn/rustc_testrunner_tmpdir_reporting.2yoSVIHoUPEC/mvce.rs:1:5
  |
1 | use Bar::foo;
  |     ^^^^^^^^
  |
  = note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information
  = help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable
  = note: this compiler was built on 2025-01-17; consider upgrading it if it is out of date

error: internal compiler error: compiler/rustc_resolve/src/late.rs:4021:18: unexpected resolution for an identifier in pattern: Def(AssocFn, DefId(0:5 ~ mvce[144b]::Bar::foo))
 --> /tmp/icemaker_global_tempdir.Z7Hkc1ETV8nn/rustc_testrunner_tmpdir_reporting.2yoSVIHoUPEC/mvce.rs:8:9
  |
8 |     let foo: Arc<dyn Foo> = propagate2(&j);
  |         ^^^


thread 'rustc' panicked at compiler/rustc_resolve/src/late.rs:4021:18:
Box<dyn Any>
stack backtrace:
   0:     0x7edbcc0f683a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h23c1af5b93b45c4e
   1:     0x7edbcc812d66 - core::fmt::write::he6ce13e554e79ce6
   2:     0x7edbcd795b91 - std::io::Write::write_fmt::hec6f02e202dc5849
   3:     0x7edbcc0f6692 - std::sys::backtrace::BacktraceLock::print::hc275ab55e4e7012b
   4:     0x7edbcc0f8b12 - std::panicking::default_hook::{{closure}}::ha505ca355cb30c7e
   5:     0x7edbcc0f899a - std::panicking::default_hook::h19ac97dfcd221a6d
   6:     0x7edbcb25a9fb - std[31774604a6320310]::panicking::update_hook::<alloc[371b401ffbdd17ea]::boxed::Box<rustc_driver_impl[6ffd8ce43d36b65a]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7edbcc0f9693 - std::panicking::rust_panic_with_hook::h5c0132368c99678a
   8:     0x7edbcb296341 - std[31774604a6320310]::panicking::begin_panic::<rustc_errors[98a8ad297a61928c]::ExplicitBug>::{closure#0}
   9:     0x7edbcb28b246 - std[31774604a6320310]::sys::backtrace::__rust_end_short_backtrace::<std[31774604a6320310]::panicking::begin_panic<rustc_errors[98a8ad297a61928c]::ExplicitBug>::{closure#0}, !>
  10:     0x7edbcb287f7d - std[31774604a6320310]::panicking::begin_panic::<rustc_errors[98a8ad297a61928c]::ExplicitBug>
  11:     0x7edbcb2a0271 - <rustc_errors[98a8ad297a61928c]::diagnostic::BugAbort as rustc_errors[98a8ad297a61928c]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7edbcb7f7c9c - <rustc_errors[98a8ad297a61928c]::DiagCtxtHandle>::span_bug::<rustc_span[2051e39db7245042]::span_encoding::Span, alloc[371b401ffbdd17ea]::string::String>
  13:     0x7edbcb87d537 - rustc_middle[d6b3b4678fa8d253]::util::bug::opt_span_bug_fmt::<rustc_span[2051e39db7245042]::span_encoding::Span>::{closure#0}
  14:     0x7edbcb862d5a - rustc_middle[d6b3b4678fa8d253]::ty::context::tls::with_opt::<rustc_middle[d6b3b4678fa8d253]::util::bug::opt_span_bug_fmt<rustc_span[2051e39db7245042]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7edbcb862beb - rustc_middle[d6b3b4678fa8d253]::ty::context::tls::with_context_opt::<rustc_middle[d6b3b4678fa8d253]::ty::context::tls::with_opt<rustc_middle[d6b3b4678fa8d253]::util::bug::opt_span_bug_fmt<rustc_span[2051e39db7245042]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x7edbca225da7 - rustc_middle[d6b3b4678fa8d253]::util::bug::span_bug_fmt::<rustc_span[2051e39db7245042]::span_encoding::Span>
  17:     0x7edbcd49a91e - <rustc_ast[a21bb4063d307b10]::ast::Pat>::walk::<<rustc_resolve[ef634132eb20349a]::late::LateResolutionVisitor>::resolve_pattern_inner::{closure#0}>
  18:     0x7edbcd49aceb - <rustc_resolve[ef634132eb20349a]::late::LateResolutionVisitor>::resolve_pattern_top
  19:     0x7edbcd493282 - <rustc_resolve[ef634132eb20349a]::late::LateResolutionVisitor>::resolve_block
  20:     0x7edbcd49c9ea - <rustc_resolve[ef634132eb20349a]::late::LateResolutionVisitor as rustc_ast[a21bb4063d307b10]::visit::Visitor>::visit_fn
  21:     0x7edbcd4b42fd - rustc_ast[a21bb4063d307b10]::visit::walk_item_ctxt::<rustc_resolve[ef634132eb20349a]::late::LateResolutionVisitor, rustc_ast[a21bb4063d307b10]::ast::ItemKind>
  22:     0x7edbcd4bd17b - <rustc_resolve[ef634132eb20349a]::late::LateResolutionVisitor as rustc_ast[a21bb4063d307b10]::visit::Visitor>::visit_item
  23:     0x7edbcda2778a - <rustc_resolve[ef634132eb20349a]::Resolver>::resolve_crate::{closure#0}
  24:     0x7edbcda2377f - <rustc_resolve[ef634132eb20349a]::Resolver>::resolve_crate
  25:     0x7edbccdcd1d7 - rustc_interface[d0fa5b755e7f54c8]::passes::resolver_for_lowering_raw
  26:     0x7edbccdcc227 - rustc_query_impl[61d446cd41769aac]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[61d446cd41769aac]::query_impl::resolver_for_lowering_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d6b3b4678fa8d253]::query::erase::Erased<[u8; 16usize]>>
  27:     0x7edbccdcc215 - <rustc_query_impl[61d446cd41769aac]::query_impl::resolver_for_lowering_raw::dynamic_query::{closure#2} as core[a9419c698344facd]::ops::function::FnOnce<(rustc_middle[d6b3b4678fa8d253]::ty::context::TyCtxt, ())>>::call_once
  28:     0x7edbcd764cb6 - rustc_query_system[2ef7fec5ec5d5e8e]::query::plumbing::try_execute_query::<rustc_query_impl[61d446cd41769aac]::DynamicConfig<rustc_query_system[2ef7fec5ec5d5e8e]::query::caches::SingleCache<rustc_middle[d6b3b4678fa8d253]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[61d446cd41769aac]::plumbing::QueryCtxt, false>
  29:     0x7edbcd764960 - rustc_query_impl[61d446cd41769aac]::query_impl::resolver_for_lowering_raw::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7edbcd7c03b0 - rustc_interface[d0fa5b755e7f54c8]::passes::create_and_enter_global_ctxt::<core[a9419c698344facd]::option::Option<rustc_interface[d0fa5b755e7f54c8]::queries::Linker>, rustc_driver_impl[6ffd8ce43d36b65a]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  31:     0x7edbcd801456 - rustc_interface[d0fa5b755e7f54c8]::interface::run_compiler::<(), rustc_driver_impl[6ffd8ce43d36b65a]::run_compiler::{closure#0}>::{closure#1}
  32:     0x7edbcd7325c7 - std[31774604a6320310]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[d0fa5b755e7f54c8]::util::run_in_thread_with_globals<rustc_interface[d0fa5b755e7f54c8]::util::run_in_thread_pool_with_globals<rustc_interface[d0fa5b755e7f54c8]::interface::run_compiler<(), rustc_driver_impl[6ffd8ce43d36b65a]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  33:     0x7edbcd732299 - <<std[31774604a6320310]::thread::Builder>::spawn_unchecked_<rustc_interface[d0fa5b755e7f54c8]::util::run_in_thread_with_globals<rustc_interface[d0fa5b755e7f54c8]::util::run_in_thread_pool_with_globals<rustc_interface[d0fa5b755e7f54c8]::interface::run_compiler<(), rustc_driver_impl[6ffd8ce43d36b65a]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[a9419c698344facd]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  34:     0x7edbcd731a2f - std::sys::pal::unix::thread::Thread::new::thread_start::h1ff917b0bfabc673
  35:     0x7edbc7aa339d - <unknown>
  36:     0x7edbc7b2849c - <unknown>
  37:                0x0 - <unknown>

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: please make sure that you have updated to the latest nightly

note: rustc 1.86.0-nightly (bb3fa4b48 2025-01-17) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [resolver_for_lowering_raw] getting the resolver for lowering
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.

@matthiaskrgr matthiaskrgr 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 Jan 17, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 17, 2025
@matthiaskrgr
Copy link
Member Author

mvce I think

use A::b;

trait A {
    fn b() {}
}

fn main() {
    let b: ();
}

@matthiaskrgr
Copy link
Member Author

bisects to #134754

@frank-king can you make it so that we don't ICE when the feature is not enabled with a feature gate for now?

@frank-king
Copy link
Contributor

I think we should just let the associated functions be silently shadowed here, just like non-associated functions

Res::Def(DefKind::Fn, _) | Res::Local(..) | Res::Err => {

And a similar issue I found is the associated constants:

use Num::{ZERO, one};

trait Num {
    const ZERO: Self;
}

impl Num for u32 {
    const ZERO: u32 = 0;
}

fn foo(x: u32) -> u32 {
    // AssocConst in patterns and expressions
    if let ZERO = x { ZERO } else { 1 }
}

fn main() {}

@frank-king
Copy link
Contributor

@matthiaskrgr The original code example you provided is syntactically incorrect (but no ICE if #135663 is applied), could you please have a check?

Code
// Check that more complex receivers work:
// terminator of the MIR, which has had the PostAnalysisNormalize pass performed on it.
//@ error-pattern: Uninitialized value was created by an allocation

//@ revisions: cfi kcfi

//@ only-linux
//@ [cfi] needs-sanitizer-cfi
//@ [kcfi] needs-sanitizer-kcfi
//@ compile-flags: -C target-feature=-crt-static
//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi
//@ [cfi] compile-flags: -Z sanitizer=cfi
//@ [kcfi] compile-flags: -Z sanitizer=kcfi
//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off
//@ run-pass

use Bar::foo;

trait Foo {
    fn foo(self: Arc<Self>)(&self)
}

struct FooImpl;

impl Foo for C {
    fn rpit_fn(self: Iter<Self>) {}
}

trait Bar {
    fn foo(&self) {}
}

struct BarImpl;

impl Bar for BarImpl {
    type T = i32;
    fn bar(&self) -> Self::T { 7 }
}

fn main() {
    let foo: Arc<dyn Foo> = propagate2(&j);
    foo.as_ptr();

    let bar: &dyn Yprintln!("p1")T=i32> = &BarImpl
    assert_eq!(use_closure_once.bar(), 3);
}
compiler stderr
error: expected one of `->`, `;`, `where`, or `{`, found `(`
  --> $DIR/resolve-issue-135614.rs:20:28
   |
LL | trait Foo {
   |           - while parsing this item list starting here
LL |     fn foo(self: Arc<Self>)(&self)
   |                            ^ expected one of `->`, `;`, `where`, or `{`
LL | }
   | - the item list ends here

error: expected one of `(`, `+`, `::`, `;`, `<`, or `=`, found `!`
  --> $DIR/resolve-issue-135614.rs:44:27
   |
LL |     let bar: &dyn Yprintln!("p1")T=i32> = &BarImpl
   |            -              ^ expected one of `(`, `+`, `::`, `;`, `<`, or `=`
   |            |
   |            while parsing the type for `bar`

error[E0658]: `use` associated items of traits is unstable
  --> $DIR/resolve-issue-135614.rs:17:5
   |
LL | use Bar::foo;
   |     ^^^^^^^^
   |
   = note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information
   = help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0407]: method `rpit_fn` is not a member of trait `Foo`
  --> $DIR/resolve-issue-135614.rs:26:5
   |
LL |     fn rpit_fn(self: Iter<Self>) {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `Foo`

error[E0437]: type `T` is not a member of trait `Bar`
  --> $DIR/resolve-issue-135614.rs:36:5
   |
LL |     type T = i32;
   |     ^^^^^^^^^^^^^ not a member of trait `Bar`

error[E0407]: method `bar` is not a member of trait `Bar`
  --> $DIR/resolve-issue-135614.rs:37:5
   |
LL |     fn bar(&self) -> Self::T { 7 }
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `Bar`

error[E0412]: cannot find type `C` in this scope
  --> $DIR/resolve-issue-135614.rs:25:14
   |
LL | impl Foo for C {
   |              ^ not found in this scope

error[E0412]: cannot find type `Iter` in this scope
  --> $DIR/resolve-issue-135614.rs:26:22
   |
LL |     fn rpit_fn(self: Iter<Self>) {}
   |                      ^^^^ not found in this scope
   |
help: consider importing one of these structs
   |
LL + use std::collections::binary_heap::Iter;
   |
LL + use std::collections::btree_map::Iter;
   |
LL + use std::collections::btree_set::Iter;
   |
LL + use std::collections::hash_map::Iter;
   |
     and 9 other candidates

error[E0412]: cannot find type `Arc` in this scope
  --> $DIR/resolve-issue-135614.rs:41:14
   |
LL |     let foo: Arc<dyn Foo> = propagate2(&j);
   |              ^^^ not found in this scope
   |
help: consider importing this struct
   |
LL + use std::sync::Arc;
   |

error[E0425]: cannot find value `j` in this scope
  --> $DIR/resolve-issue-135614.rs:41:41
   |
LL |     let foo: Arc<dyn Foo> = propagate2(&j);
   |                                         ^ not found in this scope

warning: unused import: `Bar::foo`
  --> $DIR/resolve-issue-135614.rs:17:5
   |
LL | use Bar::foo;
   |     ^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error[E0220]: associated type `T` not found for `Self`
  --> $DIR/resolve-issue-135614.rs:37:28
   |
LL |     fn bar(&self) -> Self::T { 7 }
   |                            ^ associated type `T` not found

error[E0425]: cannot find function `propagate2` in this scope
  --> $DIR/resolve-issue-135614.rs:41:29
   |
LL |     let foo: Arc<dyn Foo> = propagate2(&j);
   |                             ^^^^^^^^^^ not found in this scope

error: aborting due to 12 previous errors; 1 warning emitted

Some errors have detailed explanations: E0220, E0407, E0412, E0425, E0437, E0658.
For more information about an error, try `rustc --explain E0220`.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 18, 2025
…errors

Fix ICE in resolving associated items as non-bindings

Fixes rust-lang#135614 so that imported associated functions of traits can be shadowed by local bindings and associated constants of traits can be used in patterns.
@bors bors closed this as completed in dd13d6f Jan 19, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 19, 2025
Rollup merge of rust-lang#135663 - frank-king:fix/135614, r=compiler-errors

Fix ICE in resolving associated items as non-bindings

Fixes rust-lang#135614 so that imported associated functions of traits can be shadowed by local bindings and associated constants of traits can be used in patterns.
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 20, 2025
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