Skip to content

Wrong diagnostic when using constant of TAIT type in pattern #102011

Description

@oli-obk

The test src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs (copied below to preserve its state)

#![feature(type_alias_impl_trait)]

type Bar = impl Send;

// While i32 is structural-match, we do not want to leak this information.
// (See https://github.com/rust-lang/rust/issues/72156)
const fn leak_free() -> Bar {
    7i32
}
const LEAK_FREE: Bar = leak_free();

fn leak_free_test() {
    match LEAK_FREE {
        LEAK_FREE => (),
        //~^ ERROR constant pattern depends on a generic parameter
        //~| ERROR constant pattern depends on a generic parameter
        _ => (),
    }
}

fn main() {}

reports an error about "depends on a generic parameter", when there are no generic parameters anywhere. The issue is that we only have a single TooGeneric variant in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/interpret/enum.ErrorHandled.html#variant.TooGeneric . We should change that fieldless variant to TooGeneric(TooGeneric) and introduce an

enum TooGeneric {
    /// The query encountered a generic parameter and could
    /// not extract required information from it.
    Param,
    /// The query encountered an opaque type in `Reveal::Userfacing`
    /// mode and was thus unable to reveal the hidden type.
    OpaqueType,
}

Originally posted by @lcnr in #101478 (comment)

Activity

  1. moved this to Can do after stabilization in type alias impl trait stabilizationon Sep 19, 2022
  2. lcnr commented on Sep 19, 2022

    @lcnr
    Contributor

    i do think we should experiment with adding the too generic type to TooGeneric, i tried splitting that variant which was more annoying than helpful

  3. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Sep 20, 2022
  4. inquisitivecrystal commented on Sep 20, 2022

    @inquisitivecrystal
    Contributor

    Triage note: I was going to add one or more of the D- labels, but wasn't sure which ones were appropriate. The OP and linked post don't really help me to understand what's going on and why it's a problem. I'm sure they're perfectly comprehensible to those who understand the situation better than I do, but a little bit of elaboration might help make this issue clearer. :)

  5. oli-obk commented on Sep 20, 2022

    @oli-obk
    ContributorAuthor

    Oof, yea, sorry about that. I added some explanation including what should actually be done.

  6. added
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Sep 20, 2022
  7. added
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    C-bugCategory: This is a bug.
    and removed
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Sep 20, 2022
  8. lcnr commented on Sep 20, 2022

    @lcnr
    Contributor

    my idea was

    pub enum ErrorHandled<'tcx> {
        Reported(ErrorGuaranteed),
        Linted,
        TooGeneric(Ty<'tcx>),
    }

    Ty<'tcx> might not be perfect, e.g. sometimes we fail to resolve an instance, so there using Ty<'tcx> is wrong 🤔 maybe TooGeneric(&'tcx TooGenericCause<'tcx>) with TooGenericCause<'tcx> being an enum with the variants we care about 🤔

    this should give us pretty good error messages with a hopefully acceptable perf impact

  9. compiler-errors commented on Oct 22, 2022

    @compiler-errors
    Contributor

    This no longer reports the same error as the issue, and instead just says:

    error: `Bar` cannot be used in patterns
      --> src/main.rs:15:9
       |
    15 |         LEAK_FREE => (),
       |         ^^^^^^^^^
    
    warning: unreachable pattern
      --> src/main.rs:16:9
       |
    15 |         LEAK_FREE => (),
       |         --------- matches any value
    16 |         _ => (),
       |         ^ unreachable pattern
       |
       = note: `#[warn(unreachable_patterns)]` on by default
    
    warning: `playground` (bin "playground") generated 1 warning
    error: could not compile `playground` due to previous error; 1 warning emitted
    
  10. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    and removed
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Oct 22, 2022
  11. JohnTitor commented on Jan 5, 2023

    @JohnTitor
    Member

    Triage: Since we already have a test as src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs, I think we could just close this issue. Closing.

  12. moved this from Can do after stabilization to Done in type alias impl trait stabilizationon Jan 5, 2023
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

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions