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

correct spans in parem_env to deduplicate errors #102502

Closed
wants to merge 3 commits into from

Conversation

SparrowLii
Copy link
Member

This is part of the workaround for #102185.
param_env will set all spans to same when normalizing predictions, which is inconsistent with the behavior in typeck, causing the same error to appear twice, and cannot be deduplicated because the spans are different.
This PR has two commits. The first one is to optimize the calculation process of param_env to reduce the number of times to collect Vecs. The second is to use correct spans when normalize predictions of Fn items, so that errors generated is consistent with ones from typeck, so that they can be deduplicated.

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

r? @cjgillot

(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 Sep 30, 2022
traits::normalize_param_env_or_error(tcx, unnormalized_env, cause)
let span = tcx.def_span(def_id);

if is_fn {
Copy link
Member

Choose a reason for hiding this comment

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

I'm skeptical that all this extra code is worthwhile... since all it's here for is to deduplicate a diagnostic that's still going to end up being an error. Especially because normalize_param_env_or_error is very delicate logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for pointing it out. If that isn't worthwhile, could you take a look at the first commit, which is just a little optimization of param_env, and I hope it's acceptable?

Copy link
Member

@compiler-errors compiler-errors Oct 3, 2022

Choose a reason for hiding this comment

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

Are you sure this is an optimization in reality? Maybe you should put it up as a separate commit (separate from the span modifications) so we can do a perf run on it.

I am still not confident that the added complexity is worth it.

@@ -235,7 +237,15 @@ fn do_normalize_predicates<'tcx>(
// them here too, and we will remove this function when
// we move over to lazy normalization *anyway*.
tcx.infer_ctxt().ignoring_regions().enter(|infcx| {
let predicates = match fully_normalize(&infcx, cause, elaborated_env, predicates) {
let predicates = match predicates.try_map_id(|predicate| {
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 make this a separate local variable?
Would this be simpler as a loop mutating predicates?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for reviewing! I renamed it to normalized_predicates. I think It maybe not easy to use a mutating, since fully_normalize needs to take ownership of the elements in predicts. Similar methods that act on instances of TypeFoldable have this feature, and try_map_id is used to implement TypeFoldable for Vec.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, could you make a separate variable for the try_map_id call?

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay. I renamed it to origin_predicate. We can pick what we want and sqaush them to one commit I think

@@ -262,7 +272,15 @@ fn do_normalize_predicates<'tcx>(
);
}

match infcx.fully_resolve(predicates) {
match predicates.try_map_id(|predicate| {
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise.

@@ -219,6 +220,7 @@ fn do_normalize_predicates<'tcx>(
cause: ObligationCause<'tcx>,
elaborated_env: ty::ParamEnv<'tcx>,
predicates: Vec<ty::Predicate<'tcx>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

From the use site, should we just take &mut?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, as mentioned above, fully_normalize requires taking ownership of the predicats. I could not think of a good way to deal with this for a while.

@@ -219,6 +220,7 @@ fn do_normalize_predicates<'tcx>(
cause: ObligationCause<'tcx>,
elaborated_env: ty::ParamEnv<'tcx>,
predicates: Vec<ty::Predicate<'tcx>>,
outlives: bool,
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 make the parameter name more explicit? I don't understand what this toggles.

Copy link
Member Author

@SparrowLii SparrowLii Oct 1, 2022

Choose a reason for hiding this comment

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

Okay. In param_env, normalization will be called twice according to whether it is a predicate of type TypeOutlives. This variable is used to distinguish between the two calls.

@bors
Copy link
Contributor

bors commented Oct 7, 2022

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

@SparrowLii
Copy link
Member Author

as @compiler-errors said, The calculation of param_env is very sensitive, so adding complexity to deduplicate error messages is not worth the effort. So I think this PR can be closed. Thanks so much for @compiler-errors and @cjgillot 's reviewing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants