-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for
Self
not respecting tuple Ctor privacy
This fixes #111220 by checking the privacy of tuple constructors using `Self`, so the following code now errors ```rust mod my { pub struct Foo(&'static str); } impl AsRef<str> for my::Foo { fn as_ref(&self) -> &str { let Self(s) = self; // previously compiled, now errors correctly s } } ```
- Loading branch information
Showing
7 changed files
with
126 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
tests/ui/privacy/issue-111220-2-tuple-struct-fields-projection.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,33 @@ | ||
mod b { | ||
pub struct A(u32); | ||
} | ||
|
||
trait Id { | ||
type Assoc; | ||
} | ||
impl Id for b::A { | ||
type Assoc = b::A; | ||
} | ||
impl Id for u32 { | ||
type Assoc = u32; | ||
} | ||
|
||
|
||
trait Trait<T> { | ||
fn method(&self) | ||
where | ||
T: Id<Assoc = b::A>; | ||
} | ||
|
||
impl<T: Id> Trait<T> for <T as Id>::Assoc { | ||
fn method(&self) | ||
where | ||
T: Id<Assoc = b::A>, | ||
{ | ||
let Self(a) = self; | ||
//~^ ERROR: tuple struct constructor `A` is private | ||
println!("{a}"); | ||
} | ||
} | ||
|
||
fn main() {} |
9 changes: 9 additions & 0 deletions
9
tests/ui/privacy/issue-111220-2-tuple-struct-fields-projection.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,9 @@ | ||
error[E0603]: tuple struct constructor `A` is private | ||
--> $DIR/issue-111220-2-tuple-struct-fields-projection.rs:27:13 | ||
| | ||
LL | let Self(a) = self; | ||
| ^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0603`. |
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,46 @@ | ||
mod b { | ||
#[derive(Default)] | ||
pub struct A(u32); | ||
} | ||
|
||
impl b::A { | ||
fn inherent_bypass(&self) { | ||
let Self(x) = self; | ||
//~^ ERROR: tuple struct constructor `A` is private | ||
println!("{x}"); | ||
} | ||
} | ||
|
||
pub trait B { | ||
fn f(&self); | ||
} | ||
|
||
impl B for b::A { | ||
fn f(&self) { | ||
let Self(a) = self; | ||
//~^ ERROR: tuple struct constructor `A` is private | ||
println!("{}", a); | ||
} | ||
} | ||
|
||
pub trait Projector { | ||
type P; | ||
} | ||
|
||
impl Projector for () { | ||
type P = b::A; | ||
} | ||
|
||
pub trait Bypass2 { | ||
fn f2(&self); | ||
} | ||
|
||
impl Bypass2 for <() as Projector>::P { | ||
fn f2(&self) { | ||
let Self(a) = self; | ||
//~^ ERROR: tuple struct constructor `A` is private | ||
println!("{}", a); | ||
} | ||
} | ||
|
||
fn main() {} |
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,21 @@ | ||
error[E0603]: tuple struct constructor `A` is private | ||
--> $DIR/issue-111220-tuple-struct-fields.rs:8:13 | ||
| | ||
LL | let Self(x) = self; | ||
| ^^^^^^^ | ||
|
||
error[E0603]: tuple struct constructor `A` is private | ||
--> $DIR/issue-111220-tuple-struct-fields.rs:20:13 | ||
| | ||
LL | let Self(a) = self; | ||
| ^^^^^^^ | ||
|
||
error[E0603]: tuple struct constructor `A` is private | ||
--> $DIR/issue-111220-tuple-struct-fields.rs:40:13 | ||
| | ||
LL | let Self(a) = self; | ||
| ^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0603`. |