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

Suggest = to == in more cases, even in the face of reference mismatch #119328

Merged
merged 1 commit into from Dec 27, 2023

Conversation

estebank
Copy link
Contributor

Given foo: &String and bar: str, suggest == when given if foo = bar {}:

error[E0308]: mismatched types
  --> $DIR/assignment-expected-bool.rs:37:8
   |
LL |     if foo = bar {}
   |        ^^^^^^^^^ expected `bool`, found `()`
   |
help: you might have meant to compare for equality
   |
LL |     if foo == bar {}
   |             +

@rustbot
Copy link
Collaborator

rustbot commented Dec 26, 2023

r? @compiler-errors

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 26, 2023
@estebank estebank marked this pull request as ready for review December 26, 2023 21:15
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me after fixing

@@ -1131,8 +1131,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap();
let lhs_ty = self.check_expr(lhs);
let rhs_ty = self.check_expr(rhs);
let might_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper is named pretty vaguely -- it's not checking if lhs and rhs might coerce, it's checking if the lhs and rhs's refs can coerce.

Suggested change
let might_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It's weird because from the user's point of view it is Ty, but auto-deref is at play in binops (which is why I peel all refs and add one layer after).

compiler/rustc_hir_typeck/src/expr.rs Outdated Show resolved Hide resolved
(Applicability::MaybeIncorrect, self.can_coerce(rhs_ty, actual_lhs_ty))
(
Applicability::MaybeIncorrect,
self.can_coerce(rhs_ty, actual_lhs_ty) | might_coerce(rhs_ty, actual_lhs_ty),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

binary ||

Suggested change
self.can_coerce(rhs_ty, actual_lhs_ty) | might_coerce(rhs_ty, actual_lhs_ty),
self.can_coerce(rhs_ty, actual_lhs_ty) || might_coerce(rhs_ty, actual_lhs_ty),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been making this mistake often lately, I wonder what's causing that 🤔

Maybe that I write alternative patterns way more often than logical ors? No idea 🤷

compiler/rustc_hir_typeck/src/expr.rs Outdated Show resolved Hide resolved
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 26, 2023
…atch

Given `foo: &String` and `bar: str`, suggest `==` when given `if foo = bar {}`:

```
error[E0308]: mismatched types
  --> $DIR/assignment-expected-bool.rs:37:8
   |
LL |     if foo = bar {}
   |        ^^^^^^^^^ expected `bool`, found `()`
   |
help: you might have meant to compare for equality
   |
LL |     if foo == bar {}
   |             +
```
@estebank
Copy link
Contributor Author

@bors r=compiler-errors

@bors
Copy link
Contributor

bors commented Dec 27, 2023

📌 Commit dc30eb1 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 27, 2023
@bors
Copy link
Contributor

bors commented Dec 27, 2023

⌛ Testing commit dc30eb1 with merge eee93d8...

@bors
Copy link
Contributor

bors commented Dec 27, 2023

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing eee93d8 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 27, 2023
@bors bors merged commit eee93d8 into rust-lang:master Dec 27, 2023
12 checks passed
@rustbot rustbot added this to the 1.77.0 milestone Dec 27, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (eee93d8): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-19.2% [-22.7%, -15.3%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -19.2% [-22.7%, -15.3%] 3

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.6% [2.6%, 2.6%] 1
Improvements ✅
(primary)
-2.8% [-2.8%, -2.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.8% [-2.8%, -2.8%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
-15.5% [-19.0%, -13.4%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -15.5% [-19.0%, -13.4%] 3

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 669.634s -> 671.325s (0.25%)
Artifact size: 312.43 MiB -> 312.40 MiB (-0.01%)

@estebank
Copy link
Contributor Author

The perf improvement is spurious: #118431 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants