Skip to content

Erroneous compile errors when returning impl trait #140545

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

Closed
chlobes opened this issue May 1, 2025 · 3 comments · Fixed by #140568
Closed

Erroneous compile errors when returning impl trait #140545

chlobes opened this issue May 1, 2025 · 3 comments · Fixed by #140568
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@chlobes
Copy link

chlobes commented May 1, 2025

Code

trait Foo {}
fn a(x: impl Foo) -> impl Foo {
    if true { x } else { a(a(x)) }
}

more examples on the playground

Meta

Triggers ICE on stable, beta, and nightly 1.87.
On nightly 1.88 no ICE but a strange compile error instead

rustc --version --verbose:

rustc 1.87.0-nightly (1aeb99d24 2025-03-19)
binary: rustc
commit-hash: 1aeb99d248e1b0069110cb03c6f1dcc7b36fd7f3
commit-date: 2025-03-19
host: aarch64-apple-darwin
release: 1.87.0-nightly
LLVM version: 20.1.0

Error output

nightly:

29 | fn a(x: impl Foo) -> impl Foo {
   |         -------- this generic parameter must be used with a generic type parameter
30 |     if true { x } else { a(a(x)) }
   |                          ^^^^^^^

error: type parameter `impl Foo` is part of concrete type but not used in parameter list for the `impl Trait` type alias
  --> src/lib.rs:30:26
   |
30 |     if true { x } else { a(a(x)) }
   |                          ^^^^^^^
Backtrace

  |
  = note: delayed at compiler/rustc_middle/src/ty/opaque_types.rs:191:25
             0: <rustc_errors::DiagCtxtInner>::emit_diagnostic
             1: <rustc_errors::DiagCtxtHandle>::emit_diagnostic
             2: <rustc_span::ErrorGuaranteed as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
             3: <rustc_errors::DiagCtxtHandle>::span_delayed_bug::<rustc_span::span_encoding::Span, &str>
             4: <rustc_middle::ty::opaque_types::ReverseMapper as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_ty
             5: <rustc_middle::ty::OpaqueHiddenType>::remap_generic_params_to_declaration_params
             6: rustc_hir_analysis::collect::type_of::type_of_opaque
             7: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::type_of_opaque::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
             8: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefIdCache<rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
             9: rustc_query_impl::query_impl::type_of_opaque::get_query_non_incr::__rust_end_short_backtrace
            10: rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::DefIdCache<rustc_middle::query::erase::Erased<[u8; 8]>>>
            11: rustc_hir_analysis::collect::type_of::type_of
            12: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::type_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
            13: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefIdCache<rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
            14: rustc_query_impl::query_impl::type_of::get_query_non_incr::__rust_end_short_backtrace
            15: rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::DefIdCache<rustc_middle::query::erase::Erased<[u8; 8]>>>
            16: rustc_hir_analysis::check::check::check_item_type
            17: rustc_hir_analysis::check::wfcheck::check_well_formed
            18: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::check_well_formed::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 1]>>
            19: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_data_structures::vec_cache::VecCache<rustc_span::def_id::LocalDefId, rustc_middle::query::erase::Erased<[u8; 1]>, rustc_query_system::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
            20: rustc_query_impl::query_impl::check_well_formed::get_query_non_incr::__rust_end_short_backtrace
            21: rustc_middle::query::plumbing::query_ensure_error_guaranteed::<rustc_data_structures::vec_cache::VecCache<rustc_span::def_id::LocalDefId, rustc_middle::query::erase::Erased<[u8; 1]>, rustc_query_system::dep_graph::graph::DepNodeIndex>, ()>
            22: rustc_hir_analysis::check::wfcheck::check_mod_type_wf
            23: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::check_mod_type_wf::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 1]>>
            24: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::LocalModDefId, rustc_middle::query::erase::Erased<[u8; 1]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
            25: rustc_query_impl::query_impl::check_mod_type_wf::get_query_non_incr::__rust_end_short_backtrace
            26: rustc_hir_analysis::check_crate
            27: rustc_interface::passes::run_required_analyses
            28: rustc_interface::passes::analysis
            29: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>
            30: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
            31: rustc_query_impl::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
            32: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
            33: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
            34: std::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
            35: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
            36: std::sys::pal::unix::thread::Thread::new::thread_start
            37: <unknown>
            38: clone

@chlobes chlobes 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 1, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 1, 2025
@jieyouxu jieyouxu added E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels May 1, 2025
@theemathas
Copy link
Contributor

Related to #139406

cc @lcnr

@theemathas
Copy link
Contributor

The poor error message in nightly is a variant of #139350.

@lcnr lcnr added the fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. label May 1, 2025
@moxian
Copy link
Contributor

moxian commented May 1, 2025

This was (accidentally?) fixed in #138785, but without new regression test for this case, AFAICT

Originally regressed in nightly-2024-02-06, likely #120556 ?

full PR list for 2024-02-06
 - 2024-02-04: #120624 (Rollup of 8 pull requests) by matthiaskrgr
    - #120484 (Avoid ICE when is_val_statically_known is not of a supported type)
    - #120516 (pattern_analysis: cleanup manual impls)
    - #120517 (never patterns: It is correct to lower `!` to `_`.)
    - #120523 (Improve `io::Read::read_buf_exact` error case)
    - #120528 (Store SHOULD_CAPTURE as AtomicU8)
    - #120529 (Update data layouts in custom target tests for LLVM 18)
    - #120531 (Remove a bunch of `has_errors` checks that have no meaningful or the wrong effect)
    - #120533 (Correct paths for hexagon-unknown-none-elf platform doc)
 - 2024-02-04: #120649 (Rollup of 8 pull requests) by matthiaskrgr
    - #119759 (Add FileCheck annotations to dataflow-const-prop tests)
    - #120323 (On E0277 be clearer about implicit `Sized` bounds on type params and assoc types)
    - #120473 (Only suggest removal of `as_*` and `to_` conversion methods on E0308)
    - #120540 (add test for try-block-in-match-arm)
    - #120547 (`#![feature(inline_const_pat)]` is no longer incomplete)
    - #120552 (Correctly check `never_type` feature gating)
    - #120555 (put pnkfelix (me) back on the review queue.)
    - #120556 (Improve the diagnostics for unused generic parameters)
 - 2024-02-05: #120497 (Move predicate, region, and const stuff into their own modules in middle) by compiler-errors
 - 2024-02-05: #120508 (Update cargo) by weihanglo
 - 2024-02-05: #120660 (Rollup of 9 pull requests) by matthiaskrgr
    - #119481 (Clarify ambiguity in select_nth_unstable docs)
    - #119600 (Remove outdated references to librustc_middle)
    - #120458 (Document `&CStr` to `CString` conversion)
    - #120569 (coverage: Improve handling of function/closure spans)
    - #120572 (Update libc to 0.2.153)
    - #120587 (miri: normalize struct tail in ABI compat check)
    - #120607 (fix #120603 by adding a check in default_read_buf)
    - #120636 (Subtree update of `rust-analyzer`)
    - #120641 (rustdoc: trait.impl, type.impl: sort impls to make it not depend on serialization order)
 - 2024-02-05: #120671 (Rollup of 8 pull requests) by matthiaskrgr
    - #113833 (`std::error::Error` -> Trait Implementations: lifetimes consistency improvement)
    - #115386 (PartialEq, PartialOrd: update and synchronize handling of transitive chains)
    - #116284 (make matching on NaN a hard error, and remove the rest of illegal_floating_point_literal_pattern)
    - #118960 (Add LocalWaker and ContextBuilder types to core, and LocalWake trait to alloc.)
    - #120384 (Use `<T, U>` for array/slice equality `impl`s)
    - #120518 (riscv only supports split_debuginfo=off for now)
    - #120657 (Remove unused struct)
    - #120661 (target: default to the medium code model on LoongArch targets)
 - 2024-02-05: #117372 (Update stdarch submodule) by Amanieu
 - 2024-02-05: #120313 (pattern_analysis: Gracefully abort on type incompatibility) by Nadrieril

@rustbot label: +E-needs-test -E-needs-bisection -needs-triage

@rustbot rustbot added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 1, 2025
moxian added a commit to moxian/rust that referenced this issue May 1, 2025
moxian added a commit to moxian/rust that referenced this issue May 2, 2025
bors added a commit to rust-lang-ci/rust that referenced this issue May 3, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#139343 (Change signature of File::try_lock and File::try_lock_shared)
 - rust-lang#140505 (linker: Quote symbol names in .def files)
 - rust-lang#140534 (PassWrapper: adapt for llvm/llvm-project@f137c3d592e96330e450a8fd63ef…)
 - rust-lang#140546 (Remove backtrace dep from anyhow in features status dump tool)
 - rust-lang#140548 (Emit user type annotations for free consts in pattern position)
 - rust-lang#140564 (Use present indicative tense in std::io::pipe() API docs)
 - rust-lang#140568 (Add regression test for rust-lang#140545)
 - rust-lang#140606 (Improve hir pretty printing)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 5f9330c May 3, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 3, 2025
Rollup merge of rust-lang#140568 - moxian:reg-140545, r=compiler-errors

Add regression test for rust-lang#140545

Closes rust-lang#140545

I am not very knowledgable about the typesystem internals, so I couldn't come up with a good name for the test. But I'm happy to move it to a more appropriate place if there is one (`tests/ui/impl-trait/non-defining-uses` maybe?)

r? types (or reroll as appropriate if this is not actually a T-types issue; i'm clueless)
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. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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.

6 participants