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

When encountering struct fn call literal with private fields, suggest all builders #117683

Merged
merged 14 commits into from
Nov 19, 2023

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Nov 7, 2023

When encountering code like Box(42), suggest Box::new(42) and all other associated functions that return -> Box<T>.

Add a way to give pre-sorted suggestions.

@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2023

r? @cjgillot

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

@rustbot rustbot added A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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 Nov 7, 2023
@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2023

rustc_macros::diagnostics was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

@rustbot
Copy link
Collaborator

rustbot commented Nov 8, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

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

Couple nits.

Also, are all those sorts required? A bit surprising we have so much non-determinism...

compiler/rustc_errors/src/diagnostic.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

compiler/rustc_hir_typeck/src/expr.rs Outdated Show resolved Hide resolved
}
})
.collect::<Vec<_>>();
items.sort_by_key(|(order, _, _)| *order);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we give higher priority to methods with fewer inputs?

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 feel like definition order is a more reliable heuristic

compiler/rustc_hir_typeck/src/expr.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/expr.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/expr.rs Outdated Show resolved Hide resolved
@@ -2740,6 +2824,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.join(".")
})
.collect::<Vec<_>>();
candidate_fields.sort();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a FIXME so we don't forget to track down the non determinism?

  • all places where you had to call sort.

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 sorted in all these to maintain the current behavior, I haven't validated that all of them needed to be sorted.

@@ -1682,6 +1696,167 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
true
}

fn suggest_alternative_construction_methods(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not fond of having twice (almost) identical code in two parts of the compiler. Which version has the most effect? The two tests I see exercise the non-local version, so iiuc this one.

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 not ideal, but we have different amount of context on each stage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one affects only the first cases in tests/ui/privacy/suggest-box-new.rs, while the other affects the latter cases in that file. We have no way of knowing what the prevalence of these errors are in the wild. I you'd prefer I moved the logic to a central place I can do that. I didn't because there was no natural place where these wouldn't feel out of place.

@bors
Copy link
Contributor

bors commented Nov 16, 2023

☔ The latest upstream changes (presumably #117875) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Nov 18, 2023

☔ The latest upstream changes (presumably #118046) made this pull request unmergeable. Please resolve the merge conflicts.

@cjgillot
Copy link
Contributor

Let's go with this version, to be refactored later.
@bors r+

@bors
Copy link
Contributor

bors commented Nov 19, 2023

📌 Commit ac56b06 has been approved by cjgillot

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 Nov 19, 2023
@bors
Copy link
Contributor

bors commented Nov 19, 2023

⌛ Testing commit ac56b06 with merge 9a66e44...

@bors
Copy link
Contributor

bors commented Nov 19, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 9a66e44 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 19, 2023
@bors bors merged commit 9a66e44 into rust-lang:master Nov 19, 2023
11 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Nov 19, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9a66e44): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

Max RSS (memory usage)

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

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)
3.4% [3.1%, 3.6%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 675.906s -> 676.7s (0.12%)
Artifact size: 313.79 MiB -> 313.73 MiB (-0.02%)

span.shrink_to_hi().with_hi(expr_span.hi()),
format!("you might have meant to use the `{name}` associated function"),
suggestion(name, *args),
Applicability::MaybeIncorrect,
Copy link

Choose a reason for hiding this comment

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

shouldn't this be if args.len() > 0 { HasPlaceholders } else { MaybeIncorrect }? I was under the impression that was more severe than MaybeIncorrect

Copy link
Member

Choose a reason for hiding this comment

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

MaybeIncorrect is actually more severe than HasPlaceholders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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.

8 participants