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

[WIP] rustc_typeck: check well-formedness of type aliases. #54033

Closed
wants to merge 2 commits into from

Conversation

eddyb
Copy link
Member

@eddyb eddyb commented Sep 7, 2018

Enforce that for type aliases (type Foo<T: Trait> = Bar<T>;), all the bounds required by the type being aliased (Bar<T>) are satisfied (e.g. by T: Trait).

On Rust 2015, this is reported as a lint, but on Rust 2018 it's a hard error.
(see #49441 (comment) for more details on the decision taken)

NOTE: this is only the first half, we also need to check declared bounds assuming the RHS type is WF (to ensure the type alias is fully equivalent to its RHS type, everywhere it's used).
EDIT: second half is up at #54090.

Fixes #44075, fixes #51626.

r? @nikomatsakis

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 7, 2018
@eddyb

This comment has been minimized.

@bors

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@eddyb eddyb force-pushed the alias-waffles branch 2 times, most recently from 4c9ffde to a3f11cf Compare September 7, 2018 16:47
@rust-highfive

This comment has been minimized.

eddyb added a commit to eddyb/ena that referenced this pull request Sep 7, 2018
nikomatsakis added a commit to rust-lang/ena that referenced this pull request Sep 7, 2018
Add bounds to type aliases (needed by rust-lang/rust#54033).
@eddyb
Copy link
Member Author

eddyb commented Sep 7, 2018

@bors try (for crater in check mode)

@bors
Copy link
Contributor

bors commented Sep 7, 2018

⌛ Trying commit 013d7f0634496b2d2defd8acce43fcbed1163526 with merge 0ed22d1874786c4c6a348e14674cad1dc19df6a0...

@rust-highfive

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Sep 8, 2018

💔 Test failed - status-travis

@bors bors 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 Sep 8, 2018
@rust-highfive

This comment has been minimized.

@eddyb
Copy link
Member Author

eddyb commented Sep 8, 2018

Okay, so serde is already a problem there, which means we can't easily do the crater run I wanted to do. I'll move to warn on Rust 2015 and only error on Rust 2018.

@Centril
Copy link
Contributor

Centril commented Sep 8, 2018

@eddyb yeah that sounds reasonable.

@eddyb
Copy link
Member Author

eddyb commented Sep 17, 2018

@bors try

@bors
Copy link
Contributor

bors commented Sep 17, 2018

⌛ Trying commit 0568e27db5f862d27f210c0dac637870a4c209de with merge ecd082c0984889e4676a3dd794753309c854ab46...

@Mark-Simulacrum
Copy link
Member

Mark-Simulacrum commented Sep 17, 2018

@craterbot command follows: run start=master#cb6d2dfa8923902b0992a1522dc4a45a9d3ba690 end=try#cfeeefe83c83bceb55d60d7e2be173db7597ed50 mode=check-only

@eddyb
Copy link
Member Author

eddyb commented Sep 17, 2018

(just pushed test fixes only, try build shouldn't care)

@bors
Copy link
Contributor

bors commented Sep 17, 2018

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

@eddyb
Copy link
Member Author

eddyb commented Sep 17, 2018

@craterbot run start=master#cb6d2dfa8923902b0992a1522dc4a45a9d3ba690 end=try#cfeeefe83c83bceb55d60d7e2be173db7597ed50 mode=check-only

@craterbot
Copy link
Collaborator

👌 Experiment pr-54033 created and queued.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🚧 Experiment pr-54033 is now running on agent aws-1.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 17, 2018
let local_id_root = tables.local_id_root?;
assert!(local_id_root.is_local());

tables.node_types().iter().filter_map(|(&local_id, &node_ty)| {
Copy link
Contributor

Choose a reason for hiding this comment

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

such ECS.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: It'd be nice to pull this out into some sort of helper function, I think? I'd also like to see a comment, something like:


Iterate over the all the things that we've assigned types to. Check whether they represent local variables and, if so, if the type of that local variable references the type we are looking for.


let span = obligation.cause.span;
let lint = self.get_lint_from_cause_code(&obligation.cause.code);
macro_rules! struct_span_err_or_lint {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I feel like I've seen this macro at least twice. Can't we abstract this somehow? Or move it?

Copy link
Member Author

Choose a reason for hiding this comment

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

It specifically takes advantage of variables in scope, so sadly it wouldn't be that easy.
We'd have to take both infcx and obligation.cause (instead of tcx.sess and span), I think? Maybe it is easy.

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you think struct_obligation_err_or_lint, as a name for the macro?
Also, is it just me or is struct just not the best word to use?
I almost want to move everything to diagnostic builders so we can get rid of the non-struct_ versions and remove the struct_ prefix.

pub fn report_selection_error(&self,
obligation: &PredicateObligation<'tcx>,
error: &SelectionError<'tcx>,
fallback_has_occurred: bool)
{
let span = obligation.cause.span;

let lint = self.get_lint_from_cause_code(&obligation.cause.code);
macro_rules! struct_span_err_or_lint {
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes 3 times :)

@@ -50,7 +50,6 @@ use syntax_pos::{DUMMY_SP, Span};
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
pub fn report_fulfillment_errors(&self,
errors: &[FulfillmentError<'tcx>],
body_id: Option<hir::BodyId>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I take it we're getting the body_id from the ObligationCause here? I had planned to remove that field once we transition to NLL, since it won't be needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

But I guess we could thread this info back through then.

Copy link
Member Author

Choose a reason for hiding this comment

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

This was only used for the code in need_type_info.rs that was visiting the HIR so thankfully I think we won't need it!

I actually couldn't visit the HIR because the body_id on the obligation was sometimes "not large enough" to include the closure that had the offending argument.

@craterbot
Copy link
Collaborator

🎉 Experiment pr-54033 is completed!
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Sep 18, 2018
@eddyb
Copy link
Member Author

eddyb commented Sep 18, 2018

See #49441 (comment) - TL;DR we can't make "not enough bounds" an error in Rust 2018.
However, "too many bounds" (currently in #54090) is definitely doable, so I'll focus on that.

fcx.body_id,
p,
span));
&cause,
Copy link
Member Author

Choose a reason for hiding this comment

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

This is fine, but fcx.normalize_associated_types_in(span, &predicates) above should be fcx.inh.normalize_associated_types_in(cause, fcx.param_env, &predicates) instead.

@TimNN TimNN added A-allocators Area: Custom and system allocators and removed A-allocators Area: Custom and system allocators labels Sep 25, 2018
@TimNN
Copy link
Contributor

TimNN commented Oct 16, 2018

Ping from triage @eddyb: What is the status of this PR?

@TimNN TimNN 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 Oct 16, 2018
@Centril Centril added P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-nominated labels Oct 19, 2018
@nikomatsakis
Copy link
Contributor

I'm going to close this PR for now. I do plan to start opening some PRs and issuing warnings, per the plan discussed in #55222

@apiraino apiraino removed P-high High priority I-nominated labels Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Trait bounds are not checked on type aliases until they are used Check the well-formed-ness of type aliases.
9 participants