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

Fix erroneous span for borrowck error #98022

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

compiler-errors
Copy link
Member

I am not confident that this is the correct fix, but it does the job. Open to suggestions for a real fix instead.

Fixes #97997

The issue is that we pass a dummy location when type-checking the "required consts" that are needed by the MIR body during borrowck. This means that when we fail to evaluate the constant, we use the span of bb0[0], instead of the actual span of the constant.

There are quite a few other places that use START_BLOCK.start_location(), Location::START, etc. when calling for a random/unspecified Location value. This is because, unlike (for example) Span, we don't have a dummy/miscellaneous value to use instead. I would appreciate guidance (either in this PR, or a follow-up) on what needs to be done to clean this up in general.

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

r? @wesleywiser

(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 Jun 12, 2022
@bugadani
Copy link
Contributor

bugadani commented Jun 12, 2022

Correct me if I'm wrong, but I don't think this PR in its current state fixes #97997 as that issue is about the duplicate error, not the wrong location. Looking at the new test, this PR expect the error to be duplicated, which means merging this shouldn't mark the issue fixed.

@compiler-errors
Copy link
Member Author

@bugadani: The error is deduplicated by the diagnostic printing logic, except for in UI tests, and renders as one if you invoke rustc normally.

@compiler-errors
Copy link
Member Author

compiler-errors commented Jun 12, 2022

The reason it shows up as two separate errors when you include that extra unrelated line in the original github issue was precisely because the spans for the two copies of the error (that are always printed) differ, meaning that they don't get deduplicated by the compiler.

@wesleywiser
Copy link
Member

This seems like a question for the diagnostics working group with regards to how we want to handle this kind of situation generally in the compiler. Forwarding to @estebank as the lead of that wg.

r? @estebank

@compiler-errors
Copy link
Member Author

I was more curious about getting feedback from someone who's an expert on MIR than an expert on diagnostics. My understanding is that this isn't really a diagnosics issue per se, but an issue with Location type which is a MIR concept.

Comment on lines +360 to +366
// HACK(compiler-errors): Constants that are gathered into Body.required_consts
// have their locations erased...
let locations = if location != Location::START {
location.to_locations()
} else {
Locations::All(constant.span)
};
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like you've identified the right course of action (modifying Body.required_consts instead of here).

I'd be ok with landing this as a temporary hack, but I'd rather getting sign off from somebody immediately related to the const generics effort. I'd punt to @oli-obk, but he'll be somewhat unavailable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should just unconditionally use the constant's span, it seems like that span will always be better than what we can guess from the location (or we'll just end up fetching the constant's span at the location anyway). At which point it begs the question of whether we should be passing a location to visit_constant at all.

A quick skim of all visit_constant and visit_const suggest that we're only really interested in the span, not the actual mir location.

anyway. Leaving the hack comment and fixing this in a follow up PR seems the quickest way forward.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I looked into that refactoring, but we also use Location (and not Locations) as a key to some different HashMaps in different MIR stages? I'll see if I can give the refactoring another go, though.

@compiler-errors
Copy link
Member Author

r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned estebank Jun 21, 2022
@oli-obk
Copy link
Contributor

oli-obk commented Jun 21, 2022

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 21, 2022

📌 Commit ccf6124 has been approved by oli-obk

@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 Jun 21, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jun 21, 2022
…span, r=oli-obk

Fix erroneous span for borrowck error

I am not confident that this is the correct fix, but it does the job. Open to suggestions for a real fix instead.

Fixes rust-lang#97997

The issue is that we pass a [dummy location](https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_middle/mir/visit.rs.html#302) when type-checking the ["required consts"](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Body.html#structfield.required_consts) that are needed by the MIR body during borrowck. This means that when we fail to evaluate the constant, we use the span of `bb0[0]`, instead of the actual span of the constant.

There are quite a few other places that use `START_BLOCK.start_location()`, `Location::START`, etc. when calling for a random/unspecified `Location` value. This is because, unlike (for example) `Span`, we don't have a dummy/miscellaneous value to use instead. I would appreciate guidance (either in this PR, or a follow-up) on what needs to be done to clean this up in general.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 21, 2022
Rollup of 11 pull requests

Successful merges:

 - rust-lang#94033 (Improve docs for `is_running` to explain use case)
 - rust-lang#97269 (adjust transmute const stabilization version)
 - rust-lang#97805 (Add proper tracing spans to rustc_trait_selection::traits::error_reporting)
 - rust-lang#98022 (Fix erroneous span for borrowck error)
 - rust-lang#98124 (Improve loading of crates.js and sidebar-items.js)
 - rust-lang#98278 (Some token stream cleanups)
 - rust-lang#98306 (`try_fold_unevaluated` for infallible folders)
 - rust-lang#98313 (Remove lies in comments.)
 - rust-lang#98323 (:arrow_up: rust-analyzer)
 - rust-lang#98329 (Avoid an ICE and instead let the compiler report a useful error)
 - rust-lang#98330 (update ioslice docs to use shared slices)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a7f789b into rust-lang:master Jun 21, 2022
@rustbot rustbot added this to the 1.63.0 milestone Jun 21, 2022
@compiler-errors compiler-errors deleted the erroneous-borrowck-span branch August 11, 2023 20:00
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.

Duplicated implementation is not general enough error on function pointers
8 participants