Skip to content

Weird error in trait with HRTB for the same trait on Self in a method. Also bad diagnostic with nonsensical suggestion. #84435

Open
@steffahn

Description

@steffahn
Member

Given the following code:

trait Trait<'a>: Sized {
    fn method(self)
    where
        for<'b> Self: Trait<'b>;
}

(playground)

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0283]: type annotations needed
 --> src/lib.rs:1:1
  |
1 | trait Trait<'a>: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
  | |
  | cannot infer type for type parameter `Self`
  | required by this bound in `Trait`
  |
  = note: cannot satisfy `Self: Trait<'a>`
help: consider specifying the type arguments in the function call
  |
1 | trait Trait<'a>: Sized::<Self, 'a> {
  |                       ^^^^^^^^^^^^

error: aborting due to previous error

I would’ve expected this code to compile. Even if there is a reason why it shouldn’t compile, the error message is about the most confusing/nonsensical error message I’ve ever seen. The “best” part is how it suggests adding arguments to Sized.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 22, 2021
steffahn

steffahn commented on Apr 22, 2021

@steffahn
MemberAuthor

This code used to compile fine in rust 1.0 through 1.3, it used to ICE from 1.4 to 1.6 and it gives some kind of cannot resolve `Self : Trait<'a>`​ / E0283 error ever since 1.7.

SNCPlay42

SNCPlay42 commented on Apr 22, 2021

@SNCPlay42
Contributor

This looks similar to #84398, but that one's a recent regression.

added
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Apr 22, 2021
steffahn

steffahn commented on Apr 23, 2021

@steffahn
MemberAuthor

In 1.3-1.4 the ICE appears at nightly-2015-08-15.

> rustc +nightly-2015-08-14 --version
rustc 1.4.0-nightly (e2bebf32f 2015-08-14)
> rustc +nightly-2015-08-15 --version
rustc 1.4.0-nightly (ab450ef22 2015-08-14)

This range includes

033e127066c0d57b36eb7e202840387a98b017f8: Auto merge of #27786 - alexcrichton:start-testing-msvc, r=brson
741a19f8efcb060febfaf29bee13860a55ba8bf2: Auto merge of #27816 - steveklabnik:for_chrisbot, r=alexcrichton
e907fab17812d1f6749a4a50ae248b205a7eca0f: Auto merge of #27821 - dhuseby:manual_snapshots_1af31d4, r=alexcrichton
0649b16ade40d1fb02ea5c390293278bd876854a: Auto merge of #27822 - arielb1:inline-round-take-2, r=Gankro
e7261f3ab60e0d1e6c808004ecd25c88e04f3683: Auto merge of #27641 - nikomatsakis:soundness-rfc-1214, r=nrc
c1e865c9df3c6928525025e410fc3d165fb97c85: Auto merge of #27838 - AlisdairO:diagnostics221, r=Manishearth
ab450ef22b08241513734f73952a4a1315738b03: Auto merge of #27400 - alexcrichton:less-jemalloc, r=brson

In this list, I would suspect that #27641 could be the regression point.


In 1.6-1.7 the ICE disappears and the first version of the error message appears at nightly-2015-12-19.

> rustc +nightly-2015-12-14 --version
rustc 1.7.0-nightly (110df043b 2015-12-13)
> rustc +nightly-2015-12-19 --version
rustc 1.7.0-nightly (8ad12c3e2 2015-12-19)

That’s a huge range with no nightly versions existing in between…

6b3a3f270219819f8f98c2b6807ff70b92a941ac: Auto merge of #30369 - jethrogb:patch-2, r=steveklabnik
50a02b43ba17bf1547b7124dff909604e967a7f6: Auto merge of #29735 - Amanieu:asm_indirect_constraint, r=pnkfelix
9ea4b4f01f54ec741509c624a8aa62d7a47f7eac: Auto merge of #30321 - sanxiyn:E0170, r=alexcrichton
f150c178ea8275fab158bc7fe2a3b5af9ab8f3fe: Auto merge of #27937 - DiamondLovesYou:llvm-root-and-shared, r=alexcrichton
44d9601ad3b388df34b7e7b9a1a1bfed32b4bce9: Auto merge of #30328 - sanxiyn:include-type-name, r=alexcrichton
8f031bf96205ed4cb990c2c7aded84d5ac079254: Auto merge of #30105 - faineance:master, r=nrc
9e63cecb10a3a22abed9c230c5daf268dc258bf3: Auto merge of #30233 - retep998:where-in-the-world-is-windows-sdk, r=alexcrichton
9ace0a46de9b47cfa93c4e906805455b221e2881: Auto merge of #30141 - oli-obk:fix/30117, r=arielb1
ac2c5ff0241bdd090af11a74566bd00f03ae8ae0: Auto merge of #30206 - petrochenkov:newdepr, r=brson
ce7bc51933e2facb4eca029ac17b398f372f5b41: Auto merge of #30300 - sanxiyn:syntax-ext, r=nikomatsakis
785a8a6681963ff389b5902e7d6bd30006fafe0a: Auto merge of #30410 - Manishearth:rollup, r=Manishearth
d4ffaf6f836e6ff8260548041bd1dc9d8bd146f9: Auto merge of #30269 - sanxiyn:no-mangle-generic, r=Aatch
073b0f9b852fa8b14ea4ab8c5a724043eb39fce5: Auto merge of #30337 - wesleywiser:mir_switch, r=nikomatsakis
38da1a406472cf3c675c70fe2c0b1c665fce4b16: Auto merge of #29962 - aturon:coherence-errors, r=nmatsakis
4af427881405f95b8d87efea844cbd695fb55986: Auto merge of #30341 - pnkfelix:call-site-scope, r=nikomatsakis
9687a8a96978280824a557d32b30e619f857e6a7: Auto merge of #30354 - petrochenkov:defuse, r=sanxiyn
6734dccc3149d62d36a41979424e61dce4473267: Auto merge of #30325 - jseyfried:fixes_30078, r=nrc
48700be9cbf04d798a6f6ab65c50ab0afe86bc94: Auto merge of #30445 - steveklabnik:rollup, r=steveklabnik
4eadabd9f8818c562446751a0ef03ea2165e7056: Auto merge of #29907 - nagisa:mir-moar-constants, r=nikomatsakis
27d551142f9e3cd2fc402555a8f6287dd0fc01ac: Auto merge of #30414 - alexcrichton:fix-cross-builds, r=brson
f963eb2870f6e2b972cd9d432dc0ece954ab6452: Auto merge of #30272 - tshepang:doc-drain, r=bluss
de62f9d885623318206620ddd2856a288ccd3ee4: Auto merge of #30286 - oli-obk:const_error_span, r=nikomatsakis
33916307780495fe311fe9c080b330d266f35bfb: Auto merge of #30457 - Manishearth:rollup, r=Manishearth
29ea4eef9fa6e36f40bc1f31eb1e56bf5941ee72: Auto merge of #30374 - durka:issue-30371, r=alexcrichton
ef91cdb140d7dffa4b04f42ab0bc02dc257940e3: Auto merge of #29973 - petrochenkov:privinpub, r=nikomatsakis
5dd29cc3107f7febc26ec56ed4149cd67c1f49d8: Auto merge of #30389 - nikomatsakis:rfc1214-error, r=arielb1
9e278950c2243f0d8e6638678359139a22d7e427: Auto merge of #30364 - luqmana:mir-calls, r=nikomatsakis
8ad12c3e251df6b8ed42b4d32709f4f55470a0be: Auto merge of #30381 - fhahn:memchr-in-std, r=alexcrichton

Looking through this, it might be #30389 since that’s related to RFC 1214 again.


Skimming the RFC and also in general thinking about this code a few times I don’t see any reason why this shouldn’t be allowed to be accepted by the compiler. So I’ll consider this a bug. @rustbot modify labels: C-bug, A-traits, A-lifetimes, D-confusing, A-suggestion-diagnostics.

The labels are quite diverse now, since this could count as 3 distinct issues:

  • The fact that the code is rejected being a bug (C-bug)
  • The incorrect suggestion to add parameters to the supertrait (D-invalid-suggestion)
  • The rather nonsensical statement “cannot infer type for type parameter Self”. I don’t know of any situation where this kind of error message makes any sense; shouldn’t the compiler never actually have to “infer Self”? (D-confusing)

Even though the code used to compile pre-1.4, I’m hesitant about marking this as a regression. This is super old and RFC 1214 seems to have come with some other – deliberate – breaking changes anyways.

added
A-lifetimesArea: Lifetimes / regions
C-bugCategory: This is a bug.
D-confusingDiagnostics: Confusing error or lint that should be reworked.
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
on Apr 23, 2021
ChayimFriedman2

ChayimFriedman2 commented on Apr 12, 2022

@ChayimFriedman2
Contributor

Another case of this bug (https://users.rust-lang.org/t/strange-type-annotations-needed-error/74117?u=chrefr):

trait Trait<'a> {}
trait Foo<'a>: Trait<'a> + for<'b> Trait<'b> {}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=94818406f96dfe5f1a086ccd01cf35a7

error[E0283]: type annotations needed
 --> src/lib.rs:2:16
  |
2 | trait Foo<'a>: Trait<'a> + for<'b> Trait<'b> {}
  |                ^^^^^^^^^ cannot infer type for type parameter `Self`
  |
  = note: cannot satisfy `Self: Trait<'a>`
estebank

estebank commented on Aug 3, 2023

@estebank
Contributor

Current output:

error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed: cannot satisfy `Self: Trait<'a>`
  |
note: multiple `impl`s or `where` clauses satisfying `Self: Trait<'a>` found
 --> src/lib.rs:1:1
  |
1 | trait Trait<'a>: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
...
4 |         for<'b> Self: Trait<'b>;
  |                       ^^^^^^^^^
error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed: cannot satisfy `Self: Trait<'a>`
 --> src/lib.rs:2:16
  |
2 | trait Foo<'a>: Trait<'a> + for<'b> Trait<'b> {}
  |                ^^^^^^^^^
  |
note: multiple `impl`s or `where` clauses satisfying `Self: Trait<'a>` found
 --> src/lib.rs:2:16
  |
2 | trait Foo<'a>: Trait<'a> + for<'b> Trait<'b> {}
  |                ^^^^^^^^^   ^^^^^^^^^^^^^^^^^

2 remaining items

Loading
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 lintsA-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-trait-systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @estebank@steffahn@SNCPlay42@fmease@ChayimFriedman2

        Issue actions

          Weird error in trait with HRTB for the same trait on `Self` in a method. Also bad diagnostic with nonsensical suggestion. · Issue #84435 · rust-lang/rust