-
Notifications
You must be signed in to change notification settings - Fork 183
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
Generate auto trait clauses for opaque type goals #468
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,69 @@ pub fn push_auto_trait_impls<I: Interner>( | |
}); | ||
} | ||
|
||
/// Leak auto traits for opaque types, just like `push_auto_trait_impls` does for structs. | ||
/// | ||
/// For example, given the following program: | ||
/// | ||
/// ```notrust | ||
/// #[auto] trait Send { } | ||
/// trait Trait { } | ||
/// struct Bar { } | ||
/// opaque type Foo: Trait = Bar | ||
/// ``` | ||
/// Checking the goal `Foo: Send` would generate the following: | ||
/// | ||
/// ```notrust | ||
/// Foo: Send :- Bar: Send | ||
/// ``` | ||
pub fn push_auto_trait_impls_opaque<I: Interner>( | ||
builder: &mut ClauseBuilder<'_, I>, | ||
auto_trait_id: TraitId<I>, | ||
opaque_id: OpaqueTyId<I>, | ||
) { | ||
debug_heading!( | ||
"push_auto_trait_impls_opaque({:?}, {:?})", | ||
auto_trait_id, | ||
opaque_id | ||
); | ||
|
||
let opaque_ty_datum = &builder.db.opaque_ty_data(opaque_id); | ||
let interner = builder.interner(); | ||
|
||
// Must be an auto trait. | ||
assert!(builder.db.trait_datum(auto_trait_id).is_auto_trait()); | ||
|
||
// Auto traits never have generic parameters of their own (apart from `Self`). | ||
assert_eq!( | ||
builder.db.trait_datum(auto_trait_id).binders.len(interner), | ||
1 | ||
); | ||
|
||
let binders = opaque_ty_datum.bound.map_ref(|b| &b.hidden_ty); | ||
builder.push_binders(&binders, |builder, hidden_ty| { | ||
let self_ty: Ty<_> = ApplicationTy { | ||
name: opaque_id.cast(interner), | ||
substitution: builder.substitution_in_scope(), | ||
} | ||
.intern(interner); | ||
|
||
// trait_ref = `OpaqueType<...>: MyAutoTrait` | ||
let auto_trait_ref = TraitRef { | ||
trait_id: auto_trait_id, | ||
substitution: Substitution::from1(interner, self_ty), | ||
}; | ||
|
||
// OpaqueType<...>: MyAutoTrait :- HiddenType: MyAutoTrait | ||
builder.push_clause( | ||
auto_trait_ref, | ||
std::iter::once(TraitRef { | ||
trait_id: auto_trait_id, | ||
substitution: Substitution::from1(interner, hidden_ty.clone()), | ||
}), | ||
); | ||
}); | ||
} | ||
|
||
/// Given some goal `goal` that must be proven, along with | ||
/// its `environment`, figures out the program clauses that apply | ||
/// to this goal from the Rust program. So for example if the goal | ||
|
@@ -161,7 +224,12 @@ fn program_clauses_that_could_match<I: Interner>( | |
|
||
if trait_datum.is_non_enumerable_trait() || trait_datum.is_auto_trait() { | ||
let self_ty = trait_ref.self_type_parameter(interner); | ||
if self_ty.bound_var(interner).is_some() | ||
|
||
if let TyData::Alias(AliasTy::Opaque(opaque_ty)) = self_ty.data(interner) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this probably wants to be the placeholder, and not the alias type, but we can leave it as "future work" for now |
||
if trait_datum.is_auto_trait() { | ||
push_auto_trait_impls_opaque(builder, trait_id, opaque_ty.opaque_ty_id) | ||
} | ||
} else if self_ty.bound_var(interner).is_some() | ||
|| self_ty.inference_var(interner).is_some() | ||
{ | ||
return Err(Floundered); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminder that we should factor the "hidden type" out from this struct and make it something that we lazilly request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would that work, actually? A method on the database to request it, or something similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, just a separate method on the db that only gets invoked when auto trait impls are actually required (that's important) would be fine, I think