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 removing the tuple struct field for the unwrapped value #99593

Merged
merged 1 commit into from
Jul 26, 2022

Conversation

TaKO8Ki
Copy link
Member

@TaKO8Ki TaKO8Ki commented Jul 22, 2022

fixes #99416

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 22, 2022
@rust-highfive
Copy link
Collaborator

r? @compiler-errors

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 22, 2022
@TaKO8Ki TaKO8Ki force-pushed the suggest-removing-tuple-struct-field branch from e01518c to 1f799ae Compare July 22, 2022 06:54
Comment on lines 289 to 293
if let ty::Adt(expected_adt, substs) = expected.kind() {
if let hir::ExprKind::Field(base, ident) = expr.kind
&& let Some(typeck_results) = self.in_progress_typeck_results
{
let base_ty = typeck_results.borrow().expr_ty(base);
Copy link
Member

@compiler-errors compiler-errors Jul 22, 2022

Choose a reason for hiding this comment

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

FnCtxt has self.typeck_results which is not Option

Suggested change
if let ty::Adt(expected_adt, substs) = expected.kind() {
if let hir::ExprKind::Field(base, ident) = expr.kind
&& let Some(typeck_results) = self.in_progress_typeck_results
{
let base_ty = typeck_results.borrow().expr_ty(base);
if let ty::Adt(expected_adt, substs) = expected.kind() {
if let hir::ExprKind::Field(base, ident) = expr.kind
{
let base_ty = self.typeck_results.borrow().expr_ty(base);

&& let Some(typeck_results) = self.in_progress_typeck_results
{
let base_ty = typeck_results.borrow().expr_ty(base);
if base_ty == expected {
Copy link
Member

Choose a reason for hiding this comment

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

Can we use something like can_eq instead?

Suggested change
if base_ty == expected {
if self.can_eq(self.param_env, base_ty, expected).is_ok() {

Copy link
Member

Choose a reason for hiding this comment

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

alternatively we could use same_type_modulo_regions, but since we have access to can_eq, it's a bit more accurate.

let base_ty = typeck_results.borrow().expr_ty(base);
if base_ty == expected {
err.span_suggestion_verbose(
expr.span.with_lo(base.span.hi()),
Copy link
Member

Choose a reason for hiding this comment

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

What does this do if base comes from a macro expansion? For example:

// run-rustfix

pub struct MyWrapper(u32);

macro_rules! my_wrapper {
  ($expr:expr) => { MyWrapper($expr) }
}

fn main() {
    some_fn(my_wrapper!(123).0); //~ ERROR mismatched types
}

fn some_fn(wrapped: MyWrapper) {
    drop(wrapped);
}

I think base.span should probably be adjusted to account for macros. Try matching if let Some(base_span) = base.span.find_ancestor_inside(expr.span) first to make sure that expr span and base span overlap.

@compiler-errors compiler-errors 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 Jul 22, 2022
@TaKO8Ki TaKO8Ki force-pushed the suggest-removing-tuple-struct-field branch from 1f799ae to f85f375 Compare July 25, 2022 08:01
@TaKO8Ki
Copy link
Member Author

TaKO8Ki commented Jul 25, 2022

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 25, 2022
@compiler-errors
Copy link
Member

Wonderful. Thanks for addressing comments.

@bors r+

@bors
Copy link
Contributor

bors commented Jul 26, 2022

📌 Commit f85f375 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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 26, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 26, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#98211 (Implement `fs::get_path` for FreeBSD.)
 - rust-lang#99353 (Slightly improve mismatched GAT where clause error)
 - rust-lang#99593 (Suggest removing the tuple struct field for the unwrapped value)
 - rust-lang#99615 (Remove some explicit `self.infcx` for `FnCtxt`, which already derefs into `InferCtxt`)
 - rust-lang#99711 (Remove reachable coverage without counters)
 - rust-lang#99718 (Avoid `&str`/`Symbol` to `String` conversions)
 - rust-lang#99720 (Sync rustc_codegen_cranelift)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit d89e99a into rust-lang:master Jul 26, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 26, 2022
@TaKO8Ki TaKO8Ki deleted the suggest-removing-tuple-struct-field branch July 26, 2022 07:59
bjorn3 pushed a commit to bjorn3/rust that referenced this pull request Aug 24, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#98211 (Implement `fs::get_path` for FreeBSD.)
 - rust-lang#99353 (Slightly improve mismatched GAT where clause error)
 - rust-lang#99593 (Suggest removing the tuple struct field for the unwrapped value)
 - rust-lang#99615 (Remove some explicit `self.infcx` for `FnCtxt`, which already derefs into `InferCtxt`)
 - rust-lang#99711 (Remove reachable coverage without counters)
 - rust-lang#99718 (Avoid `&str`/`Symbol` to `String` conversions)
 - rust-lang#99720 (Sync rustc_codegen_cranelift)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Suggest to directly use a value instead of wrapping the unwrapped value
5 participants