-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #92164 - WaffleLapkin:rustc_must_implement_one_of_att…
…r, r=Aaron1011 Implement `#[rustc_must_implement_one_of]` attribute This PR adds a new attribute — `#[rustc_must_implement_one_of]` that allows changing the "minimal complete definition" of a trait. It's similar to GHC's minimal `{-# MINIMAL #-}` pragma, though `#[rustc_must_implement_one_of]` is weaker atm. Such attribute was long wanted. It can be, for example, used in `Read` trait to make transitions to recently added `read_buf` easier: ```rust #[rustc_must_implement_one_of(read, read_buf)] pub trait Read { fn read(&mut self, buf: &mut [u8]) -> Result<usize> { let mut buf = ReadBuf::new(buf); self.read_buf(&mut buf)?; Ok(buf.filled_len()) } fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> { default_read_buf(|b| self.read(b), buf) } } impl Read for Ty0 {} //^ This will fail to compile even though all `Read` methods have default implementations // Both of these will compile just fine impl Read for Ty1 { fn read(&mut self, buf: &mut [u8]) -> Result<usize> { /* ... */ } } impl Read for Ty2 { fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> { /* ... */ } } ``` For now, this is implemented as an internal attribute to start experimenting on the design of this feature. In the future we may want to extend it: - Allow arbitrary requirements like `a | (b & c)` - Allow multiple requirements like - ```rust #[rustc_must_implement_one_of(a, b)] #[rustc_must_implement_one_of(c, d)] ``` - Make it appear in rustdoc documentation - Change the syntax? - Etc Eventually, we should make an RFC and make this (or rather similar) attribute public. --- I'm fairly new to compiler development and not at all sure if the implementation makes sense, but at least it passes tests :)
- Loading branch information
Showing
17 changed files
with
431 additions
and
4 deletions.
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
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
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
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
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
44 changes: 44 additions & 0 deletions
44
src/test/ui/traits/default-method/rustc_must_implement_one_of.rs
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
#[rustc_must_implement_one_of(eq, neq)] | ||
trait Equal { | ||
fn eq(&self, other: &Self) -> bool { | ||
!self.neq(other) | ||
} | ||
|
||
fn neq(&self, other: &Self) -> bool { | ||
!self.eq(other) | ||
} | ||
} | ||
|
||
struct T0; | ||
struct T1; | ||
struct T2; | ||
struct T3; | ||
|
||
impl Equal for T0 { | ||
fn eq(&self, _other: &Self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
impl Equal for T1 { | ||
fn neq(&self, _other: &Self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
impl Equal for T2 { | ||
fn eq(&self, _other: &Self) -> bool { | ||
true | ||
} | ||
|
||
fn neq(&self, _other: &Self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
impl Equal for T3 {} | ||
//~^ not all trait items implemented, missing one of: `eq`, `neq` | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
src/test/ui/traits/default-method/rustc_must_implement_one_of.stderr
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0046]: not all trait items implemented, missing one of: `eq`, `neq` | ||
--> $DIR/rustc_must_implement_one_of.rs:41:1 | ||
| | ||
LL | impl Equal for T3 {} | ||
| ^^^^^^^^^^^^^^^^^ missing one of `eq`, `neq` in implementation | ||
| | ||
note: required because of this annotation | ||
--> $DIR/rustc_must_implement_one_of.rs:3:1 | ||
| | ||
LL | #[rustc_must_implement_one_of(eq, neq)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0046`. |
19 changes: 19 additions & 0 deletions
19
src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
#[rustc_must_implement_one_of(a, a)] | ||
//~^ Functions names are duplicated | ||
trait Trait { | ||
fn a() {} | ||
} | ||
|
||
#[rustc_must_implement_one_of(b, a, a, c, b, c)] | ||
//~^ Functions names are duplicated | ||
//~| Functions names are duplicated | ||
//~| Functions names are duplicated | ||
trait Trait1 { | ||
fn a() {} | ||
fn b() {} | ||
fn c() {} | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.