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

Add parenthesis around suggestions to use .into() on cast expr #47699

Closed
estebank opened this issue Jan 24, 2018 · 1 comment
Closed

Add parenthesis around suggestions to use .into() on cast expr #47699

estebank opened this issue Jan 24, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@estebank
Copy link
Contributor

Given the following code:

fn main() {
    let array = [1, 2, 3];
    test(array.len() as u8);
}

fn test(length: u32) {
    println!("{}", length);
}

rustc suggests

error[E0308]: mismatched types
 --> src/main.rs:3:10
  |
3 |     test(array.len() as u8);
  |          ^^^^^^^^^^^^^^^^^ expected u32, found u8
help: you can cast an `u8` to `u32`, which will zero-extend the source value
  |
3 |     test(array.len() as u8.into());
  |          ^^^^^^^^^^^^^^^^^^^^^^^^

The suggestion should be enclosing the entire expression in parenthesis.

This was introduced in #47247. The solution is to change needs_paren to be inclusive of the precedence of as, in other words, change the < with <= and add a test for the behavior.

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-diagnostics Area: Messages for errors, warnings, and lints E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. C-bug Category: This is a bug. labels Jan 24, 2018
@petrochenkov
Copy link
Contributor

Unary prefix operators should be parenthesized as well in general case.
https://play.rust-lang.org/?gist=010eb251f1e518d682fcf7a70ba0a1db&version=undefined

As I said in #47247 AssocOp::As.precedence() needs to be replaced with PREC_POSTFIX.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 25, 2018
…nce, r=petrochenkov

Fix into() cast paren check precedence

As discussed in rust-lang#47699 the logic for determining if an expression needs parenthesis when suggesting an `.into()` cast is incorrect. Two broken examples from nightly are:

```
error[E0308]: mismatched types
 --> main.rs:4:10
  |
4 |     test(foo as i8);
  |          ^^^^^^^^^ expected i32, found i8
help: you can cast an `i8` to `i32`, which will sign-extend the source value
  |
4 |     test(foo as i8.into());
  |
```

```
error[E0308]: mismatched types
 --> main.rs:4:10
  |
4 |     test(*foo);
  |          ^^^^ expected i32, found i8
help: you can cast an `i8` to `i32`, which will sign-extend the source value
  |
4 |     test(*foo.into());
  |
```

As suggested by @petrochenkov switch the precedence check to `PREC_POSTFIX`. This catches both `as` and unary operators. Fixes rust-lang#47699.

r? @petrochenkov
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 25, 2018
…nce, r=petrochenkov

Fix into() cast paren check precedence

As discussed in rust-lang#47699 the logic for determining if an expression needs parenthesis when suggesting an `.into()` cast is incorrect. Two broken examples from nightly are:

```
error[E0308]: mismatched types
 --> main.rs:4:10
  |
4 |     test(foo as i8);
  |          ^^^^^^^^^ expected i32, found i8
help: you can cast an `i8` to `i32`, which will sign-extend the source value
  |
4 |     test(foo as i8.into());
  |
```

```
error[E0308]: mismatched types
 --> main.rs:4:10
  |
4 |     test(*foo);
  |          ^^^^ expected i32, found i8
help: you can cast an `i8` to `i32`, which will sign-extend the source value
  |
4 |     test(*foo.into());
  |
```

As suggested by @petrochenkov switch the precedence check to `PREC_POSTFIX`. This catches both `as` and unary operators. Fixes rust-lang#47699.

r? @petrochenkov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

No branches or pull requests

2 participants