-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
fix for Self
not respecting tuple Ctor privacy
#111245
fix for Self
not respecting tuple Ctor privacy
#111245
Conversation
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
compiler/rustc_privacy/src/lib.rs
Outdated
def: self.tcx.def_path_str(adt_def.did()), | ||
}); | ||
} | ||
debug!("hello"); |
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.
hi 👋
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.
waves
Do we want a crater run here? I think it wouldn't hurt to see what happens, not that we won't ship a fix but maybe we want a grace period and an FCW (which, given that this was broken forever, we could afford). Gonna start a try build for that. |
⌛ Trying commit d110c0096ebfbabed16d50f60fb008737d055238 with merge c7dcd44b00f701bf5d912cfabfad0f07883de539... |
☀️ Try build successful - checks-actions |
@craterbot check |
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
d110c00
to
587900b
Compare
🎉 Experiment
|
compiler/rustc_privacy/src/lib.rs
Outdated
@@ -1309,6 +1309,23 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { | |||
let def = match qpath { | |||
hir::QPath::Resolved(_, path) => match path.res { | |||
Res::Def(kind, def_id) => Some((kind, def_id)), | |||
// HACK: prevent using the `Self` ctor when the concrete type is private. | |||
Res::SelfCtor(impl_) => { |
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.
it's very annoying, but we normalize before looking up the constructor iirc, so the following should still be an issue:
mod b {
#[derive(Default)]
pub struct A(u32);
}
pub trait B {
fn f(&self);
}
pub trait Id {
type Assoc;
}
impl<T> Id for T {
type Assoc = Self;
}
impl B for <b::A as Id>::Assoc {
fn f(&self) {
let Self(a) = self;
//~^ ERROR: tuple struct constructor `A` is private
println!("{}", a);
}
}
fn main() {
let a = b::A::default();
a.f();
}
while ugly, it might be safer to do the check in astconv or sth 🤔 I am not familiar with this code
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.
It doesn't allow projections:
error: the `Self` constructor can only be used with tuple or unit structs
--> /home/gh-fee1-dead/rust/tests/ui/privacy/issue-111220-tuple-struct-fields.rs:28:13
|
LL | let Self(a) = self;
| ^^^^^^^
error[E0164]: expected tuple struct or tuple variant, found self constructor `Self`
--> /home/gh-fee1-dead/rust/tests/ui/privacy/issue-111220-tuple-struct-fields.rs:28:13
|
LL | let Self(a) = self;
| ^^^^^^^ not a tuple struct or tuple variant
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.
what? 😅 this compiles on nightly: https://rust.godbolt.org/z/nM6vzrc5Y
how does your code change this '^^ please add this as a test
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.
Ahh, I turned b::A::Assoc
into (b::A,)
which isn't a tuple struct lol. This is indeed an issue, I will look into this when I get time.
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 used try_normalize_erasing_regions
which should fix this for impl Trait for Foo::AssocTy
. Let me know if this should totally be at some other crate and not rustc_privacy
. I mainly put this stuff here so I can use item_is_accessible
.
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 think normalizing it with the impl where bounds is still not enough as you can avoid it as follows (please add this as a test)
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;
println!("{a}");
}
}
You would have to normalize in the param_env
of self.current_item
I think but even then it feels somewhat dangerous.
Can you add a check to instantiate_value_path
that the used ctor for is actually accessible 🤔 either completely move this check to there or at least delay_span_bug
if we expect it to error.
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, I think this could be moved to typeck.
587900b
to
65ba58e
Compare
@rustbot author |
65ba58e
to
3e715d9
Compare
Moved to typeck. @rustbot ready |
r=me after updating the PR description. I am also not sure whether I would consider this fix to be "temporary" 🤔 I guess we can mention this in the relnotes, even if it feels very unlikely that someone will depend on this. Crater did not show any regressions. This PR fixes #111220 by checking the privacy of tuple constructors using 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
}
} |
Self
not respecting tuple Ctor privacySelf
not respecting tuple Ctor privacy
3e715d9
to
dee65a4
Compare
@bors r=lcnr |
…truct-field, r=lcnr fix for `Self` not respecting tuple Ctor privacy This PR fixes rust-lang#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 } } ```
🔒 Merge conflict This pull request and the master branch diverged in a way that cannot be automatically merged. Please rebase on top of the latest master branch, and let the reviewer approve again. How do I rebase?Assuming
You may also read Git Rebasing to Resolve Conflicts by Drew Blessing for a short tutorial. Please avoid the "Resolve conflicts" button on GitHub. It uses Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Error message
|
This fixes rust-lang#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 } } ```
dee65a4
to
be44860
Compare
@bors r+ |
…truct-field, r=lcnr fix for `Self` not respecting tuple Ctor privacy This PR fixes rust-lang#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 } } ```
…truct-field, r=lcnr fix for `Self` not respecting tuple Ctor privacy This PR fixes rust-lang#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 } } ```
☀️ Test successful - checks-actions |
Finished benchmarking commit (23040c4): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 644.182s -> 645.695s (0.23%) |
This PR fixes #111220 by checking the privacy of tuple constructors using
Self
, so the following code now errors