Skip to content

Erroneous compile errors when returning impl trait #140545

Closed

Description

@chlobes

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

Activity

  1. added
    C-bugCategory: This is a bug.
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on May 1, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 1, 2025
  3. added
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
    on May 1, 2025
  4. theemathas commented on May 1, 2025

    @theemathas
    Contributor

    Related to #139406

    cc @lcnr

  5. theemathas commented on May 1, 2025

    @theemathas
    Contributor

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

  6. added
    fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.
    on May 1, 2025
  7. moxian commented on May 1, 2025

    @moxian
    Contributor

    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

  8. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    and removed
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 1, 2025
  9. added a commit that references this issue on May 3, 2025
  10. added a commit that references this issue on May 3, 2025
  11. added a commit that references this issue on May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions