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

Default type parameters are not used in type inference #99727

Closed
agluszak opened this issue Jul 25, 2022 · 3 comments
Closed

Default type parameters are not used in type inference #99727

agluszak opened this issue Jul 25, 2022 · 3 comments
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@agluszak
Copy link

agluszak commented Jul 25, 2022

I tried this code:

struct BoxyMcBoxFace<T = ()> {
    inner: Option<T>,
}

fn main() {
    let _boxy_ok1 = BoxyMcBoxFace { inner: Some(()) }; // obviously
    let _boxy_ok2: BoxyMcBoxFace<()> = BoxyMcBoxFace { inner: None };
    let _boxy_ok3: BoxyMcBoxFace = BoxyMcBoxFace { inner: None }; // that's getting more interesting
    let _boxy_err = BoxyMcBoxFace { inner: None }; // ERROR: cannot infer type...
}

I expected _boxy_err to compile (i.e. that the compiler would use the default type during inference, as it does with _boxy_ok3).

Instead, this happened:

error[E0282]: type annotations needed for `BoxyMcBoxFace<T>`
 --> src/main.rs:9:9
  |
9 |     let _boxy_err = BoxyMcBoxFace { inner: None }; // ERROR: cannot infer type...
  |         ^^^^^^^^^
  |
help: consider giving `_boxy_err` an explicit type, where the type for type parameter `T` is specified
  |
9 |     let _boxy_err: BoxyMcBoxFace<T> = BoxyMcBoxFace { inner: None }; // ERROR: cannot infer type...
  |                  ++++++++++++++++++

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

In the best case, this is working as intended (but that's unintuitive) and the suggestion is wrong ("where the type for type parameter T is specified"). But maybe that's a bug in the compiler.

If it's a bug, I can try to fix it if I get some guidance.

Meta

rustc --version --verbose:

rustc 1.62.1 (e092d0b6b 2022-07-16)
binary: rustc
commit-hash: e092d0b6b43f2de967af0887873151bb1c0b18d3
commit-date: 2022-07-16
host: x86_64-unknown-linux-gnu
release: 1.62.1
LLVM version: 14.0.5

cargo +nightly rustc -- --version --verbose:

rustc 1.64.0-nightly (7fe022f5a 2022-07-24)
binary: rustc
commit-hash: 7fe022f5aa32bbbb33c3a58755729d6667a461a9
commit-date: 2022-07-24
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

Setting the backtrace doesn't change anything in the output

@agluszak agluszak added the C-bug Category: This is a bug. label Jul 25, 2022
@agluszak agluszak changed the title Cannot infer the default type for type parameter Cannot infer the default type for type parameter in a struct Jul 25, 2022
@agluszak agluszak changed the title Cannot infer the default type for type parameter in a struct Default type parameters are not used in type inference Jul 25, 2022
@memoryruins
Copy link
Contributor

This is expected behavior #27336

The suggestion reads correctly to me, but is definitely a general one. Do you expect it to suggest the default type or something else?

yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 14, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
@Noratrieb
Copy link
Member

@agluszak can this be closed? Or do you think the error message should be improved to talk about associated type defaults and leave it open?

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference labels Jan 25, 2024
@fmease
Copy link
Member

fmease commented Jan 25, 2024

Closing in favor of #98931 / as duplicate of #27336.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Jan 25, 2024
@fmease fmease removed the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants