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

Misc HIR typeck type mismatch tweaks #112116

Merged

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented May 30, 2023

These are all intended to improve #112104, but I couldn't get it to actually suggest adding as_ref to the LHS of the equality expr without some hacks that I may play around with some more.

Each commit's title should explain what it's doing except for perhaps the last one, which addresses the bogus suggestion on #112104 itself.

@rustbot
Copy link
Collaborator

rustbot commented May 30, 2023

r? @WaffleLapkin

(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 May 30, 2023
@rust-cloud-vms rust-cloud-vms bot force-pushed the misc-hir-typeck-mismatch-tweaks branch from ce910f8 to 89032b4 Compare May 30, 2023 22:05
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

Can we add a check for the following?

let _: Result<&usize, _> = &Ok(42);

Comment on lines +1 to +8
error[E0308]: mismatched types
--> $DIR/dont-suggest-cyclic-constraint.rs:7:35
|
LL | debug_assert_eq!(iter.next(), Some(value));
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
|
= note: expected enum `Option<<I as Iterator>::Item>`
found enum `Option<&<I as Iterator>::Item>`
Copy link
Contributor

Choose a reason for hiding this comment

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

The expectation in the ticket was for this to suggest .as_ref(), right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, which is why I didn't close it just yet.

Comment on lines +26 to +27
| ------------ ^^^^ expected `Option<&String>`, found `&Option<String>`
| |
Copy link
Contributor

Choose a reason for hiding this comment

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

Sad to lose these.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll see if I can add it back

Copy link
Member Author

@compiler-errors compiler-errors May 31, 2023

Choose a reason for hiding this comment

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

Wait, I'm not actually sure what you're talking about when looking at this now 😅

For the &Some case, we still give the right suggestion below:

help: try using `.as_ref()` to convert `&Option<String>` to `Option<&String>`
   |
LL -     takes_option(&res);
LL +     takes_option(res.as_ref());

And for the &None case, we no longer suggest None.as_ref(), in favor of just suggesting removing the & which is far more accurate.

@rust-cloud-vms rust-cloud-vms bot force-pushed the misc-hir-typeck-mismatch-tweaks branch from 7a667ec to 6615862 Compare May 31, 2023 22:22
@WaffleLapkin
Copy link
Member

@compiler-errors the tests look good, thanks for the work! I'll review the code shortly...

One question though, does the code support something like this:

let _: Option<&usize> = Some(1);

I'd expect rustc to suggest .as_ref() here, since it should be fine with auto ref coercions (in this exact case it could also suggest &1, but given an opaque Option<usize> we should probably try suggesting as_ref).

@compiler-errors
Copy link
Member Author

let _: Option<&usize> = Some(1);

@WaffleLapkin: I don't think it does. I debated adding support for autoref-ish suggestions, but that case specifically has a higher likelihood than the others to lead to later borrowck errors compared to just deref-related suggestions because it may require taking a reference to a temporary or something.

@compiler-errors
Copy link
Member Author

compiler-errors commented Jun 5, 2023

Oops, correction: it does suggest y.as_ref(), but yeah, it needs to be an "opaque" option like:

let y = Some(1);
let _: Option<&usize> = y;

Which says:

help: try using `.as_ref()` to convert `Option<{integer}>` to `Option<&usize>`
  |
3 |     let _: Option<&usize> = y.as_ref();
  |                              +++++++++

Otherwise the compiler prefers to suggest Some(&1) which I do agree is a better suggestion when possible.

Comment on lines +196 to +199
help: try using `.as_ref()` to convert `&Result<{integer}, _>` to `Result<&usize, _>`
|
LL - let _: Result<&usize, _> = &Ok(42);
LL + let _: Result<&usize, _> = Ok(42).as_ref();
Copy link
Contributor

Choose a reason for hiding this comment

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

Despite the concerns of this not being idiomatic, I'm more than ok with landing with a suggestion like this.

compiler/rustc_hir_typeck/src/demand.rs Outdated Show resolved Hide resolved
Comment on lines +446 to +457
} else if let ty::Adt(adt, _) = found_ty_inner.peel_refs().kind()
&& Some(adt.did()) == self.tcx.lang_items().string()
&& peeled.is_str()
Copy link
Member

Choose a reason for hiding this comment

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

Why this checks for string specifically? (and not for example for a type with as_ref with an appropriate signature or something like that)

Copy link
Member Author

Choose a reason for hiding this comment

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

Because it's complicated, 🤷

I didn't implement this specific suggestion, just moved it around, and I don't really want to make it particularly more complicated 😅

err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
fluent::hir_typeck_convert_to_str,
".map(|x| x.as_str())",
Copy link
Member

Choose a reason for hiding this comment

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

String::as_str without the closure, maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this applies with any number of peeled refs, so this won't work:

fn f(x: Option<&&&&String>) {
  let _: Option<&str> = x;
}

@WaffleLapkin
Copy link
Member

r=me with(out) nits

@WaffleLapkin WaffleLapkin 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 Jun 8, 2023
@rust-cloud-vms rust-cloud-vms bot force-pushed the misc-hir-typeck-mismatch-tweaks branch from 6615862 to a918822 Compare June 8, 2023 16:37
@compiler-errors
Copy link
Member Author

@bors r=WaffleLapkin

@bors
Copy link
Contributor

bors commented Jun 8, 2023

📌 Commit a918822 has been approved by WaffleLapkin

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 Jun 8, 2023
@bors
Copy link
Contributor

bors commented Jun 9, 2023

⌛ Testing commit a918822 with merge 9c843d9...

@bors
Copy link
Contributor

bors commented Jun 9, 2023

☀️ Test successful - checks-actions
Approved by: WaffleLapkin
Pushing 9c843d9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 9, 2023
@bors bors merged commit 9c843d9 into rust-lang:master Jun 9, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 9, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9c843d9): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

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.7% [0.7%, 0.8%] 3
Regressions ❌
(secondary)
0.4% [0.3%, 0.7%] 11
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) 0.7% [0.7%, 0.8%] 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)
3.4% [3.4%, 3.4%] 1
Regressions ❌
(secondary)
2.0% [2.0%, 2.0%] 1
Improvements ✅
(primary)
-1.3% [-2.9%, -0.4%] 5
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) -0.5% [-2.9%, 3.4%] 6

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.5% [2.5%, 2.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 649.387s -> 649.729s (0.05%)

@rustbot rustbot added the perf-regression Performance regression. label Jun 9, 2023
@pnkfelix
Copy link
Member

  • only primary benchmark to regress was helloworld (3 incr check variants), and not by all that much (relatively speaking)
  • secondary regressions were mainly to unify-linearly, await-call-tree, token-stream-stress.
  • impact seems acceptable, marking as triaged.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Jun 13, 2023
@compiler-errors
Copy link
Member Author

Also I don't think I changed anything that isn't on the error path.

@compiler-errors compiler-errors deleted the misc-hir-typeck-mismatch-tweaks branch August 11, 2023 20:07
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. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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.

7 participants