Skip to content

Add initial implementation of HIR-based WF checking for diagnostics #83898

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jul 17, 2021

Conversation

Aaron1011
Copy link
Member

During well-formed checking, we walk through all types 'nested' in
generic arguments. For example, WF-checking Option<MyStruct<u8>>
will cause us to check MyStruct<u8> and u8. However, this is done
on a rustc_middle::ty::Ty, which has no span information. As a result,
any errors that occur will have a very general span (e.g. the
definintion of an associated item).

This becomes a problem when macros are involved. In general, an
associated type like type MyType = Option<MyStruct<u8>>; may
have completely different spans for each nested type in the HIR. Using
the span of the entire associated item might end up pointing to a macro
invocation, even though a user-provided span is available in one of the
nested types.

This PR adds a framework for HIR-based well formed checking. This check
is only run during error reporting, and is used to obtain a more precise
span for an existing error. This is accomplished by individually
checking each 'nested' type in the HIR for the type, allowing us to
find the most-specific type (and span) that produces a given error.

The majority of the changes are to the error-reporting code. However,
some of the general trait code is modified to pass through more
information.

Since this has no soundness implications, I've implemented a minimal
version to begin with, which can be extended over time. In particular,
this only works for HIR items with a corresponding DefId (e.g. it will
not work for WF-checking performed within function bodies).

@rust-highfive
Copy link
Contributor

r? @varkor

(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 Apr 5, 2021
@@ -58,6 +58,9 @@ impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
obligation: obligation.clone(),
code: FulfillmentErrorCode::CodeAmbiguity,
points_at_arg_span: false,
// FIXME - does Chalk have a notation of 'root obligation'?
Copy link
Member Author

Choose a reason for hiding this comment

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

cc @rust-lang/wg-traits: Does Chalk have a way of getting the obligation first passed to TraitEngine::register_predicate_obligation?

Copy link
Member

Choose a reason for hiding this comment

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

Well, does Chalk? No. Chalk only takes a goal and produces an answer. Does the chalk_fulfill code? I'm not sure, I'll have to go back through this.

I imagine that the code here is most correct. The rustc trait solver will register predicates as it tries to solve a predicate, whereas Chalk won't, it just gives back an answer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, chalk sort of only has a notion of root obligation, at least as presently designed.

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 too concerned because I already fully expect to have to rework a lot of things when we try to land chalk :)

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Aaron1011
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 5, 2021
@bors
Copy link
Collaborator

bors commented Apr 5, 2021

⌛ Trying commit 962fccd92d7b05b813e61caba12756a6c1ccca26 with merge 8a8a6e6b205d7dc020125389bc03483f80fee94c...

@bors
Copy link
Collaborator

bors commented Apr 5, 2021

☀️ Try build successful - checks-actions
Build commit: 8a8a6e6b205d7dc020125389bc03483f80fee94c (8a8a6e6b205d7dc020125389bc03483f80fee94c)

@rust-timer
Copy link
Collaborator

Queued 8a8a6e6b205d7dc020125389bc03483f80fee94c with parent 5a7a0ac, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (8a8a6e6b205d7dc020125389bc03483f80fee94c): comparison url.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 6, 2021
@varkor
Copy link
Member

varkor commented Apr 21, 2021

I'm a little short on time for larger reviews at the moment, so I'm going to reassign to avoid delay.

r? @estebank

@rust-highfive rust-highfive assigned estebank and unassigned varkor Apr 21, 2021
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. A-diagnostics Area: Messages for errors, warnings, and lints and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 8, 2021
@Aaron1011 Aaron1011 removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 8, 2021
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 28, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 30, 2021
@JohnCSimon JohnCSimon removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 22, 2021
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 16, 2021

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

@estebank
Copy link
Contributor

@Aaron1011 sorry for the delays. Looking at this PR now.

@Aaron1011
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 16, 2021
@bors
Copy link
Collaborator

bors commented Jul 16, 2021

⌛ Trying commit d07f0a702d7c37c08988d06ddda241720f0cad76 with merge 2b90040103d3c924f06ba922b016ccdd6e47f4b9...

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.

r=me after addressing nitpicks

@@ -58,6 +58,9 @@ impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
obligation: obligation.clone(),
code: FulfillmentErrorCode::CodeAmbiguity,
points_at_arg_span: false,
// FIXME - does Chalk have a notation of 'root obligation'?
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 too concerned because I already fully expect to have to rework a lot of things when we try to land chalk :)

@bors
Copy link
Collaborator

bors commented Jul 16, 2021

☀️ Try build successful - checks-actions
Build commit: 2b90040103d3c924f06ba922b016ccdd6e47f4b9 (2b90040103d3c924f06ba922b016ccdd6e47f4b9)

@rust-timer
Copy link
Collaborator

Queued 2b90040103d3c924f06ba922b016ccdd6e47f4b9 with parent 2119976, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (2b90040103d3c924f06ba922b016ccdd6e47f4b9): comparison url.

Summary: This benchmark run did not return any significant changes.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 16, 2021
During well-formed checking, we walk through all types 'nested' in
generic arguments. For example, WF-checking `Option<MyStruct<u8>>`
will cause us to check `MyStruct<u8>` and `u8`. However, this is done
on a `rustc_middle::ty::Ty`, which has no span information. As a result,
any errors that occur will have a very general span (e.g. the
definintion of an associated item).

This becomes a problem when macros are involved. In general, an
associated type like `type MyType = Option<MyStruct<u8>>;` may
have completely different spans for each nested type in the HIR. Using
the span of the entire associated item might end up pointing to a macro
invocation, even though a user-provided span is available in one of the
nested types.

This PR adds a framework for HIR-based well formed checking. This check
is only run during error reporting, and is used to obtain a more precise
span for an existing error. This is accomplished by individually
checking each 'nested' type in the HIR for the type, allowing us to
find the most-specific type (and span) that produces a given error.

The majority of the changes are to the error-reporting code. However,
some of the general trait code is modified to pass through more
information.

Since this has no soundness implications, I've implemented a minimal
version to begin with, which can be extended over time. In particular,
this only works for HIR items with a corresponding `DefId` (e.g. it will
not work for WF-checking performed within function bodies).
@Aaron1011
Copy link
Member Author

@bors r=estebank

@bors
Copy link
Collaborator

bors commented Jul 16, 2021

📌 Commit a765333 has been approved by estebank

@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 16, 2021
@bors
Copy link
Collaborator

bors commented Jul 16, 2021

⌛ Testing commit a765333 with merge 32c447e...

@bors
Copy link
Collaborator

bors commented Jul 17, 2021

☀️ Test successful - checks-actions
Approved by: estebank
Pushing 32c447e to master...

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.