Suggest casting on numeric type error#47247
Conversation
|
(rust_highfive has picked a reviewer for you, use r? to override) |
|
Shouldn't we suggest to use |
|
More globally, I'm not sure talking about size fit/shortage is a good idea. Just talking about |
src/librustc_typeck/check/demand.rs
Outdated
There was a problem hiding this comment.
Should "undefined behaviour" be capitalised like this? It doesn't seem to be typical in error messages.
I find the possibility of using the compiler errors to teach the language appealing. Presenting some of the possible trade offs inline like this seems like a great opportunity, even though completely I agree with the problems presented by
Part of what gave me pause is that after: That being said, the way I wrote the code was to make it possible to only show a subset of these suggestions depending on the case. I could suggest |
|
Updated the PR to only suggest |
|
That's problematic that |
|
@GuillaumeGomez in https://github.com/rust-lang/rust/pull/47247/files#diff-1743dc5c92f33d15fb1e903d7ad3ce58 (which is admittedly very verbose, as it has every single possible numeric type mismatch) you can see that the only suggestions provided are for the types that have a
|
src/librustc_typeck/check/demand.rs
Outdated
There was a problem hiding this comment.
I think you can reuse syntax::util::parser::expr_precedence here and get more precise result.
See fn print_expr_method_call in pprust.rs for an example.
src/librustc_typeck/check/demand.rs
Outdated
There was a problem hiding this comment.
float <-> float conversions are buggy?
I don't think this is true. There's -Z saturating-float-casts for correcting float -> int casts, but everything else is already correct, if I remember correctly.
There was a problem hiding this comment.
This warning is taken from https://doc.rust-lang.org/book/first-edition/casting-between-types.html#numeric-casts. Is it out of date?
There was a problem hiding this comment.
The default behavior is indeed still UB, saturating casts are still opt-in.
src/librustc_typeck/check/demand.rs
Outdated
There was a problem hiding this comment.
If into is available, then the conversion will be lossless and no rounding will happen.
Also into is not available for i/usize and floats.
There was a problem hiding this comment.
Updated message.
The suggestion is not given for isize/usize (that's what the unwrap(256) is doing). Added comment to that effect.
src/librustc_typeck/check/demand.rs
Outdated
There was a problem hiding this comment.
|
Looks pretty useful. |
85696b5 to
59cc9f7
Compare
src/librustc_typeck/check/demand.rs
Outdated
There was a problem hiding this comment.
Argh, this expr_precedence works on AST.
There's another one in rustc::hir::print.
Also, AssocOp::As.precedence() -> PREC_POSTFIX.
There was a problem hiding this comment.
I'm gonna add a commit to merge both into one place (new enum that only holds the type to precedence mapping, and an into impl for {hir, ast}::Expr so that there's only one source of truth for this. Started that yesterday but went to bed before I could complete it.
|
☔ The latest upstream changes (presumably #46461) made this pull request unmergeable. Please resolve the merge conflicts. |
- Use `syntax::util::parser::expr_precedence` to determine wether parenthesis are needed around the casting target. - Update message to not incorrectly mention rounding on `.into()` suggestions, as those types that do have that implemented will never round.
59cc9f7 to
27e9689
Compare
Introduce a new unified type that holds the expression precedence for both AST and HIR nodes.
27e9689 to
afe8d13
Compare
|
Fixed. |
src/libsyntax/ast.rs
Outdated
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum ExprPrecedence { |
There was a problem hiding this comment.
Could you move all this stuff from ast.rs somewhere, e.g. into util/parser.rs where it was previously.
This is not a part of AST or HIR, really, it's a parsing-specific detail.
There was a problem hiding this comment.
Done. Waiting for the tests to run to make sure nothing broke.
|
@bors r+ |
|
📌 Commit 71c0873 has been approved by |
Suggest casting on numeric type error Re rust-lang#47168.
Re #47168.