-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Add anti-fundamental attribute #160391
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
Draft
ShaddyDC
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
ShaddyDC:anti-fundamental
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add anti-fundamental attribute #160391
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,12 @@ pub enum InCrate { | |||||
| Remote, | ||||||
| } | ||||||
|
|
||||||
| impl InCrate { | ||||||
| pub fn def_id_is_local<I: Interner>(self, def_id: impl DefId<I>) -> bool { | ||||||
| matches!(self, InCrate::Local { .. }) && def_id.is_local() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[derive(Copy, Clone, Debug)] | ||||||
| pub enum OrphanCheckMode { | ||||||
| /// Proper orphan check. | ||||||
|
|
@@ -118,6 +124,7 @@ impl From<bool> for IsFirstInputType { | |||||
| pub enum OrphanCheckErr<I: Interner, T> { | ||||||
| NonLocalInputType(Vec<(I::Ty, IsFirstInputType)>), | ||||||
| UncoveredTyParams(UncoveredTyParams<I, T>), | ||||||
| AntiFundamentalForeignType { self_ty: I::Ty, fundamental_ty: I::Ty }, | ||||||
| } | ||||||
|
|
||||||
| #[derive_where(Debug; I: Interner, T: Debug)] | ||||||
|
|
@@ -160,6 +167,13 @@ pub struct UncoveredTyParams<I: Interner, T> { | |||||
| /// - however, `LocalType<Vec<T>>` is OK, because `T` is a subtree of | ||||||
| /// `LocalType<Vec<T>>`, which is local and has no types between it and | ||||||
| /// the type parameter. | ||||||
| /// 5. If the trait is marked `#[rustc_anti_fundamental]`, the `Self` type | ||||||
| /// must not have a non-local `#[fundamental]` type at its head (even if | ||||||
| /// it wraps a local type as in (2)). | ||||||
| /// - e.g., `Box<LocalType>` or `&Pin<LocalType>` is rejected if the trait | ||||||
| /// is `#[rustc_anti_fundamental]`. | ||||||
| /// - This lets the standard library reserve control over traits like `Deref` | ||||||
| /// and `DispatchFromDyn` on fundamental wrappers such as `Box` and `Pin`. | ||||||
| /// | ||||||
| /// The orphan rules actually serve several different purposes: | ||||||
| /// | ||||||
|
|
@@ -223,7 +237,7 @@ pub fn orphan_check_trait_ref<Infcx, I, E: Debug>( | |||||
| infcx: &Infcx, | ||||||
| trait_ref: ty::TraitRef<I>, | ||||||
| in_crate: InCrate, | ||||||
| lazily_normalize_ty: impl FnMut(I::Ty) -> Result<I::Ty, E>, | ||||||
| mut lazily_normalize_ty: impl FnMut(I::Ty) -> Result<I::Ty, E>, | ||||||
| ) -> Result<Result<(), OrphanCheckErr<I, I::Ty>>, E> | ||||||
| where | ||||||
| Infcx: InferCtxtLike<Interner = I>, | ||||||
|
|
@@ -234,6 +248,20 @@ where | |||||
| panic!("orphan check only expects inference variables: {trait_ref:?}"); | ||||||
| } | ||||||
|
|
||||||
| // Anti-fundamental check: if the trait is marked `#[rustc_anti_fundamental]`, | ||||||
| // we do not allow impls where the head of the Self type is a non-local fundamental | ||||||
| // type. This prevents downstream crates from implementing traits like `Deref` on | ||||||
| // fundamental wrappers like `Box` or `Pin`. | ||||||
| let cx = infcx.cx(); | ||||||
| if cx.trait_is_anti_fundamental(trait_ref.def_id) { | ||||||
| let self_ty = trait_ref.self_ty(); | ||||||
| if let Some(fundamental_ty) = | ||||||
| check_anti_fundamental_head(infcx, in_crate, &mut lazily_normalize_ty, self_ty)? | ||||||
| { | ||||||
| return Ok(Err(OrphanCheckErr::AntiFundamentalForeignType { self_ty, fundamental_ty })); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| let mut checker = OrphanChecker::new(infcx, in_crate, lazily_normalize_ty); | ||||||
| Ok(match trait_ref.visit_with(&mut checker) { | ||||||
| ControlFlow::Continue(()) => Err(OrphanCheckErr::NonLocalInputType(checker.non_local_tys)), | ||||||
|
|
@@ -256,6 +284,46 @@ where | |||||
| }) | ||||||
| } | ||||||
|
|
||||||
| /// Checks the head of the Self type for a non-local fundamental type. | ||||||
| /// | ||||||
| /// If the head is a reference (`&` / `&mut`), we unwrap it and inspect the pointee type. | ||||||
| /// This ensures that wrapping a fundamental type in a reference (such as `&Pin<LocalType>`) | ||||||
|
Contributor
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.
Suggested change
|
||||||
| /// cannot be used to bypass the anti-fundamental restriction. | ||||||
| /// | ||||||
| /// Returns `Some(ty)` with the offending fundamental type if the check fails. | ||||||
| fn check_anti_fundamental_head<Infcx, I, E: Debug>( | ||||||
| infcx: &Infcx, | ||||||
| in_crate: InCrate, | ||||||
| mut lazily_normalize_ty: impl FnMut(I::Ty) -> Result<I::Ty, E>, | ||||||
| mut ty: I::Ty, | ||||||
| ) -> Result<Option<I::Ty>, E> | ||||||
| where | ||||||
| Infcx: InferCtxtLike<Interner = I>, | ||||||
| I: Interner, | ||||||
| { | ||||||
| loop { | ||||||
| ty = infcx.shallow_resolve(ty); | ||||||
| let norm_ty = match lazily_normalize_ty(ty)? { | ||||||
| norm if norm.is_ty_var() => ty, | ||||||
|
Contributor
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. Huh, if |
||||||
| norm => norm, | ||||||
| }; | ||||||
|
|
||||||
| if let ty::Ref(_, inner, _) = norm_ty.kind() { | ||||||
| ty = inner; | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| ty = norm_ty; | ||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| Ok(matches!( | ||||||
| ty.kind(), | ||||||
| ty::Adt(def, _) if def.is_fundamental() && !in_crate.def_id_is_local(def.def_id()) | ||||||
| ) | ||||||
| .then_some(ty)) | ||||||
| } | ||||||
|
|
||||||
| struct OrphanChecker<'a, Infcx, I: Interner, F> { | ||||||
| infcx: &'a Infcx, | ||||||
| in_crate: InCrate, | ||||||
|
|
@@ -296,11 +364,8 @@ where | |||||
| ControlFlow::Break(OrphanCheckEarlyExit::UncoveredTyParam(ty)) | ||||||
| } | ||||||
|
|
||||||
| fn def_id_is_local(&mut self, def_id: impl DefId<I>) -> bool { | ||||||
| match self.in_crate { | ||||||
| InCrate::Local { .. } => def_id.is_local(), | ||||||
| InCrate::Remote => false, | ||||||
| } | ||||||
| fn def_id_is_local(&self, def_id: impl DefId<I>) -> bool { | ||||||
| self.in_crate.def_id_is_local(def_id) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I feel that we don't need bullet points to structure the motivation, it would be more readable if this is just a prose.
View changes since the review