-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
better error message when using dyn
type
#46683
Comments
Feels like we should change the printout to complain about |
@arielb1 also true! |
Any updates on this? |
Nope, but I definitely think this is something we should do. cc @withoutboats -- this is precisely the kind of bad |
Its a pretty interesting case. Certainly Ariel's suggestion is a strict improvement:
The most common thing people do when they see the current error message, in my experience, is add a But the real problem here is that users don't realize that Possibly the correct approach is to invert this error message for I'm not great at error messages, but the idea I think we want to express is:
|
Hmm, yes. We can certainly try to dig in and offer some tips about the declaration site of |
Maybe just something like "note that the |
Update:
|
I think this is a duplicate of #57744. |
Hi, I am very confused by this error message. I basically took the boilerplate code for cli_table and pasted it into my main fn: use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
let table = vec![
vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
]
.table()
.title(vec![
"Name".cell().bold(true),
"Age (in years)".cell().bold(true),
])
.bold(true);
assert!(print_stdout(table).is_ok()); Cool. This works, but now I want make a separate function that returns the Table and just call the final print function from main. So I have a function that returns cli_table's pub fn render_table() -> Table { Then the compile tells me I need to add So then I put the pub fn render_table() -> Box<dyn Table> { But now back main I can't just call this and use it like it's a table... let table = render_table();
assert!(print_stdout(table).is_ok()); gives me the error:
So I need to I guess "unbox" the Table now? This section of Rust by Example starts talking about it but doesn't mention at all how to actually use the Boxed Animal... 🥲 From this section of Easy Rust it says I should be able to use the star character to "deref the box back to the Trait", but when I do that I get the original "no size at compile time error" 😭 let table = render_table();
assert!(print_stdout(*table).is_ok());
UPDATE - ok, after all that I realized that there was actually another Trait exported from the third party crate that I should have been using the whole time... |
@JimLynchCodes feel free to create new tickets for these kind of issues. Sadly we are aware of the sorry state of some trait bound errors, having examples for them is useful because they help us 1) track their change over time and 2) we might be able to fix a "family" of issues when we notice patterns. For a case like this one, where importing a trait from a crate the compiler knows about is the solution, then |
The error that you get when using a
dyn
type with a fn that expects aSized
type parameter is not particularly enlightening:yields:
This came up when attempting to do some refactorings to use a
dyn Trait
where previously we had used a type parameter. I'm not exactly sure what sort of error we should report here -- but it seems like it'd be nice to talk about the definition site.The text was updated successfully, but these errors were encountered: