about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-03-11 18:13:31 +0000
committerbors <bors@rust-lang.org>2025-03-11 18:13:31 +0000
commit43a25a5ec6403530ca9a3ca088d4d126d0477578 (patch)
tree939e636997342b156079b14c4743be7c1e0104b0
parent5a44fffc336cf4cdc1c54386ea3f048254c51eef (diff)
parentd9de94001a25d33d4ba77cfa686c65f49264264c (diff)
downloadrust-43a25a5ec6403530ca9a3ca088d4d126d0477578.tar.gz
rust-43a25a5ec6403530ca9a3ca088d4d126d0477578.zip
Auto merge of #128440 - oli-obk:defines, r=lcnr
Add `#[define_opaques]` attribute and require it for all type-alias-impl-trait sites that register a hidden type

Instead of relying on the signature of items to decide whether they are constraining an opaque type, the opaque types that the item constrains must be explicitly listed.

A previous version of this PR used an actual attribute, but had to keep the resolved `DefId`s in a side table.

Now we just lower to fields in the AST that have no surface syntax, instead a builtin attribute macro fills in those fields where applicable.

Note that for convenience referencing opaque types in associated types from associated methods on the same impl will not require an attribute. If that causes problems `#[defines()]` can be used to overwrite the default of searching for opaques in the signature.

One wart of this design is that closures and static items do not have generics. So since I stored the opaques in the generics of functions, consts and methods, I would need to add a custom field to closures and statics to track this information. During a T-types discussion we decided to just not do this for now.

fixes #131298
-rw-r--r--example/issue-72793.rs24
1 files changed, 11 insertions, 13 deletions
diff --git a/example/issue-72793.rs b/example/issue-72793.rs
index 2e08fbca8ef..95d58b90e79 100644
--- a/example/issue-72793.rs
+++ b/example/issue-72793.rs
@@ -2,23 +2,21 @@
 
 #![feature(type_alias_impl_trait)]
 
-mod helper {
-    pub trait T {
-        type Item;
-    }
+pub trait T {
+    type Item;
+}
 
-    pub type Alias<'a> = impl T<Item = &'a ()>;
+pub type Alias<'a> = impl T<Item = &'a ()>;
 
-    struct S;
-    impl<'a> T for &'a S {
-        type Item = &'a ();
-    }
+struct S;
+impl<'a> T for &'a S {
+    type Item = &'a ();
+}
 
-    pub fn filter_positive<'a>() -> Alias<'a> {
-        &S
-    }
+#[define_opaque(Alias)]
+pub fn filter_positive<'a>() -> Alias<'a> {
+    &S
 }
-use helper::*;
 
 fn with_positive(fun: impl Fn(Alias<'_>)) {
     fun(filter_positive());