Change:
I tried this code: I expected to see this happen: compilation should fail because the trait impl is defined outside of the module for the type but refers to its private field. Instead, this happened: this compiled successfully This reproduces on the playground for stable Rust, version 1.69.0:
Adding a main function and attempting to call the (playground link: ) raises the compilation error: !
This can be reproduced with cross-crate (e.i., any other crate can access the private field). This means that all encapsulations that take advantage of the field being private can be bypassed. (For example, if we change Vec to a tuple struct, the same thing as setlen could be done in safe code by abusing this bug.) label +I-unsound Also, this can be reproduced in all versions where is stable (e.i., 1.32+). It's a matter of how you initialize Foo. If you provide a constructor for Foo, you won't get a compile error:
Just for completeness, turning this into a segfault without external crates/just std:
label A-visibility
this has been unsound ever since in version 1.32
WG-prioritization assigning priority (). label -I-prioritize +P-high

Discussion:
hir_typeck_convert_to_str = try converting the passed type into a `&str` <ins> hir_typeck_ctor_is_private = tuple struct constructor `{$def}` is private </ins> hir_typeck_expected_default_return_type = expected `()` because of default return type hir_typeck_expected_return_type = expected `{$expected}` because of return type

pub item_name: Ident, pub action_or_ty: String, } <ins> #[derive(Diagnostic)] #[diag(hir_typeck_ctor_is_private, code = "E0603")] pub struct CtorIsPrivate { #[primary_span] pub span: Span, pub def: String, } </ins>

use crate::callee::{self, DeferredCallResolution}; <ins> use crate::errors::CtorIsPrivate; </ins> use crate::method::{self, MethodCallee, SelfSource}; use crate::rvalue_scopes; use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LocalTy, RawTy};

match ty.normalized.ty_adt_def() { Some(adt_def) if adt_def.has_ctor() => { let (ctor_kind, ctor_def_id) = adt_def.non_enum_variant().ctor.unwrap(); <ins> // Check the visibility of the ctor. let vis = tcx.visibility(ctor_def_id); if !vis.is_accessible_from(tcx.parent_module(hir_id).to_def_id(), tcx) { tcx.sess .emit_err(CtorIsPrivate { span, def: tcx.def_path_str(adt_def.did()) }); } </ins> let new_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id); let user_substs = Self::user_substs_for_adt(ty); user_self_ty = user_substs.user_self_ty;

<ins> 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() {} </ins>

<ins> 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`. </ins>

<ins> 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() {} </ins>

<ins> 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`. </ins>