Skip to content

Bad error message trying to use Box as a tuple struct #22488

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ghost opened this issue Feb 18, 2015 · 9 comments · Fixed by #116858
Closed

Bad error message trying to use Box as a tuple struct #22488

ghost opened this issue Feb 18, 2015 · 9 comments · Fixed by #116858
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system C-bug Category: This is a bug. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@ghost
Copy link

ghost commented Feb 18, 2015

struct U <T> {
  wtf: Option<Box<U<T>>>
}
fn main() {
 U{ wtf: Some(Box(U{ wtf: None })) };
}
<anon>:5:19: 5:33 error: mismatched types:
 expected `core::ptr::Unique<_>`,
    found `U<_>`
(expected struct `core::ptr::Unique`,
    found struct `U`) [E0308]
<anon>:5  U{ wtf: Some(Box(U{ wtf: None })) };
                           ^~~~~~~~~~~~~~
<anon>:5:15: 5:34 error: mismatched types:
 expected `Box<U<_>>`,
    found `alloc::boxed::Box<_>`
(expected box,
    found struct `alloc::boxed::Box`) [E0308]
<anon>:5  U{ wtf: Some(Box(U{ wtf: None })) };
                       ^~~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101

Could someone change the bad title? I am not good for english.

@steveklabnik steveklabnik changed the title There code can't complie pass though. This code doesn't compile Feb 18, 2015
@steveklabnik steveklabnik added the A-type-system Area: Type system label Feb 18, 2015
@Gankra
Copy link
Contributor

Gankra commented Feb 18, 2015

Seems like more of a diagnostics issue. Box(..) is a private constructor, but we're erroring out on type-checking before we realize this.

The fix for this specific code is to replace Box(..) with Box::new(..)

@Gankra Gankra added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 18, 2015
@kmcallister kmcallister changed the title This code doesn't compile Bad error message trying to use Box as a tuple struct Feb 18, 2015
@steveklabnik
Copy link
Member

Triage: this now has a different, yet still confusing, error:

foo.rs:1:10: 1:11 error: parameter `T` is never used [E0392]
foo.rs:1 struct U<T> {
                  ^
foo.rs:1:10: 1:11 help: run `rustc --explain E0392` to see a detailed explanation
foo.rs:1:10: 1:11 help: consider removing `T` or using a marker such as `core::marker::PhantomData`

@Mark-Simulacrum
Copy link
Member

That error is correct; but either way, the other error you get today is this, which while not ideal, is possibly the best we can do. I guess we could look for a new method with a single parameter? But that seems hacky.

error[E0423]: expected function, found struct `Box`
 --> test.rs:5:18
  |
5 |     U{ wtf: Some(Box(U{ wtf: None })) };
  |                  ^^^
  |                  |
  |                  did you mean `Box { /* fields */ }`?
  |                  constructor is not visible here due to private fields

error: aborting due to previous error

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@estebank
Copy link
Contributor

I guess we could look for a new method with a single parameter? But that seems hacky.

It is, but it is a common enough pattern and such a big enhancement in end user experience that I feel it'd be worth it to add it, specially when the ctor is not public. Then, the output should be:

error[E0423]: expected function, found struct `Box`
 --> test.rs:5:18
  |
5 |     U{ wtf: Some(Box(U{ wtf: None })) };
  |                  ^^^ help: did you mean to use the `new` method?: `Box::new`

error: aborting due to previous error

Similar output, but not quite the same cause in #22692.

@estebank
Copy link
Contributor

estebank commented Jan 2, 2018

In order to implement this there's a bit of roadblock at the moment. The verification if a struct has a given method is done in rustc_typeck, while this error is being emitted in src/librustc_resolve/lib.rs. I believe the error is being bubbled up, but it would have to be delayed on its entirety to the type check step in order to verify wether Box::new exists. :-/

You can look for traits that have a given method (get_traits_containing_item), but when I tried it with new it returned an empty vec. We could provide with a generic suggestion about trying out new, but I would much rather prefer if we could have some certainty that the method exists instead of potentially misleading the user.

bors added a commit that referenced this issue Jan 21, 2018
Tweaks to invalid ctor messages

 - Do not suggest using a constructor that isn't accessible
 - Suggest the appropriate syntax (`()`/`{}` as appropriate)
 - Add note when trying to use `Self` as a ctor

CC #22488, fix #47085.
@estebank
Copy link
Contributor

Current output:

error[E0423]: expected function, found struct `Box`
 --> src/main.rs:5:15
  |
5 |  U{ wtf: Some(Box(U{ wtf: None })) };
  |               ^^^ constructor is not visible here due to private fields
help: possible better candidates are found in other modules, you can import them into scope
  |
1 | use syn::Expr::Box;
  |
1 | use syn::Pat::Box;
  |
1 | use syn::token::Box;
  |

error[E0392]: parameter `T` is never used
 --> src/main.rs:1:11
  |
1 | struct U <T> {
  |           ^ unused type parameter
  |
  = help: consider removing `T` or using a marker such as `std::marker::PhantomData`

error: aborting due to 2 previous errors

@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Oct 4, 2019
@Centril
Copy link
Contributor

Centril commented Mar 26, 2020

@estebank The error message seems pretty good... let's close this?

@estebank
Copy link
Contributor

@Centril I still think that we should see if any associated function would fit the bill. Definitely low priority now given the other improvements on the error.

@estebank estebank added P-low Low priority A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed D-confusing Diagnostics: Confusing error or lint that should be reworked. E-needs-mentor labels Mar 26, 2020
@JohnTitor JohnTitor added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 2, 2021
@estebank
Copy link
Contributor

estebank commented Dec 28, 2022

Current output:

error[E0423]: cannot initialize a tuple struct which contains private fields
 --> src/main.rs:7:19
  |
7 |         wtf: Some(Box(U {
  |                   ^^^
  |
note: constructor is not visible here due to private fields
 --> /rustc/92c1937a90e5b6f20fa6e87016d6869da363972e/library/alloc/src/boxed.rs:202:14
  |
  = note: private field
  |
  = note: private field
help: consider importing one of these items instead
  |
1 | use syn::Expr::Box;
  |
1 | use syn::Pat::Box;
  |
1 | use syn::token::Box;
  |

We still need to look for Box::new and suggest it.

@Dylan-DPC Dylan-DPC added C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels May 26, 2023
estebank added a commit to estebank/rust that referenced this issue Oct 17, 2023
estebank added a commit to estebank/rust that referenced this issue Oct 17, 2023
estebank added a commit to estebank/rust that referenced this issue Oct 18, 2023
estebank added a commit to estebank/rust that referenced this issue Oct 26, 2023
TaKO8Ki added a commit to TaKO8Ki/rust that referenced this issue Oct 27, 2023
Suggest assoc fn `new` when trying to build tuple struct with private fields

Fix rust-lang#22488.
TaKO8Ki added a commit to TaKO8Ki/rust that referenced this issue Oct 27, 2023
Suggest assoc fn `new` when trying to build tuple struct with private fields

Fix rust-lang#22488.
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 27, 2023
Suggest assoc fn `new` when trying to build tuple struct with private fields

Fix rust-lang#22488.
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 27, 2023
Suggest assoc fn `new` when trying to build tuple struct with private fields

Fix rust-lang#22488.
@bors bors closed this as completed in 87dc85d Oct 27, 2023
@fmease fmease added A-type-system Area: Type system and removed A-type-system Area: Type system labels Dec 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system C-bug Category: This is a bug. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants