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 soundness bug with type alias impl trait #82558

Closed
wants to merge 6 commits into from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Feb 26, 2021

cc @jackh726

fixes #73481 (only the first defining for a specific TAIT use within a function is handled at all, all others are not looked at at all)

r? @matthewjasper

I am fully aware the diagnostics aren't great, but they are ungreat in general around type alias impl trait, which is tracked in #63205

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 26, 2021
compiler/rustc_trait_selection/src/opaque_types.rs Outdated Show resolved Hide resolved
Comment on lines 21 to 33
error: defining use generics `[B, A]` differ from previous defining use
--> $DIR/multiple-def-uses-in-one-fn.rs:10:1
|
LL | fn f<A, B: 'static>(a: &'static A, b: B) -> (X<A, B>, X<B, A>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: previous defining use with different generics `[A, B]` found here
--> $DIR/multiple-def-uses-in-one-fn.rs:10:1
|
LL | fn f<A, B: 'static>(a: &'static A, b: B) -> (X<A, B>, X<B, A>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors
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 duplication is really odd.. we go through the TAIT logic (where I emit this diagnostic) twice. Once with the function as the body, once with the crate root as the body. I haven't been able to figure out the reason for that yet, but will investigate further.

@rust-log-analyzer

This comment has been minimized.

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.

I'd be ok with landing this either way (with or without the err ty) as long as we leave a ticket open to sometime fix this.

Comment on lines 1 to 11
error: defining use generics `[B, A]` differ from previous defining use
--> $DIR/multiple-def-uses-in-one-fn.rs:10:45
|
LL | fn f<A, B: 'static>(a: &'static A, b: B) -> (X<A, B>, X<B, A>) {
| ^^^^^^^^^^^^^^^^^^
|
note: previous defining use with different generics `[A, B]` found here
--> $DIR/multiple-def-uses-in-one-fn.rs:10:45
|
LL | fn f<A, B: 'static>(a: &'static A, b: B) -> (X<A, B>, X<B, A>) {
| ^^^^^^^^^^^^^^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

This is quite confusing to me :(

Copy link
Member

Choose a reason for hiding this comment

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

I think the spans could be better here: the error should have a span of X<B, A> and the note should have a span of X<A, B>

),
)
.span_note(
opaque_defn.definition_span,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we check whether this and self.value_span are the same, and if they are, emit a different note or span_label?

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 made a note of this, but it may become obsolete once I tackle all these spans in a more general manner.

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

I think this approach makes sense to me, but I don't know much about this code, so someone else (@matthewjasper) can do the "real" review.

@jackh726 jackh726 added F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804 labels Mar 2, 2021
@jackh726
Copy link
Member

jackh726 commented Mar 2, 2021

r? @nikomatsakis

@bors
Copy link
Contributor

bors commented Mar 16, 2021

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

@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 Apr 4, 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 Apr 23, 2021
@Dylan-DPC-zz
Copy link

@nikomatsakis any updates on this?

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

So, I think that this fix is sound, but it doesn't seem correct -- I think we can accommodate this sort of case without a real problem.

debug!("instantiate_opaque_types: returning concrete ty {:?}", opaque_defn.concrete_ty);
return opaque_defn.concrete_ty;
// than once in a function (e.g., if it's passed to a type alias).
if let Some(opaque_defn) = self.opaque_types.get_mut(&def_id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like the right behavior here is to key by def-id+substs. We know that the substs are always universally quantified placeholders, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The substs here are concrete types from the function. I don't understand what universally quantified placeholders mean here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keying by DefId+substs makes sense though. We'll get two type variables, and then we can figure out whether they are compatible in the same pass that does it cross-function

Copy link
Contributor

Choose a reason for hiding this comment

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

"Universally quantified placeholders" is jargon for the "generic parameters on the fn that we are type-checking". i.e., when we treat a parameter T as a kind of placeholder for "some type". "universally quantified' is because there is a "forall" quantifier on the function.

opaque_defn.substs.push(substs);
opaque_defn.concrete_ty = tcx.ty_error_with_message(
self.value_span,
"defining use generics differ from previous defining use",
Copy link
Contributor

Choose a reason for hiding this comment

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

(Just a note that I think we could say this in a clearer way; I doubt users know what a defining use is, etc)

@bors
Copy link
Contributor

bors commented May 7, 2021

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

@nikomatsakis nikomatsakis 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 May 12, 2021
@nikomatsakis
Copy link
Contributor

@spastorino is going to pick up this PR -- @spastorino do you plan to open a separate PR? Should we close this one?

@spastorino
Copy link
Member

Yes, I can open a separate PR.

@spastorino spastorino closed this May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804
Projects
Development

Successfully merging this pull request may close these issues.

Multiple defining uses of type alias impl trait in single function are not handled