Skip to content

Commit d00a89a

Browse files
author
Jonathan Turner
authored
Rollup merge of #36044 - mikhail-m1:master, r=jonathandturner
update error E0450 to new format Fixes #35925 as part of #35233. I've solve the bonus, and I wonder if any simpler way to do this. But may be possible simplify if let expressions? r? @jonathandturner
2 parents 3bab26e + 9c07ed2 commit d00a89a

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/librustc_privacy/lib.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,32 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
444444
}), ..}) => ty,
445445
_ => expr_ty
446446
}.ty_adt_def().unwrap();
447-
let any_priv = def.struct_variant().fields.iter().any(|f| {
448-
!f.vis.is_accessible_from(self.curitem, &self.tcx.map)
449-
});
450-
if any_priv {
451-
span_err!(self.tcx.sess, expr.span, E0450,
452-
"cannot invoke tuple struct constructor with private \
453-
fields");
447+
448+
let private_indexes : Vec<_> = def.struct_variant().fields.iter().enumerate()
449+
.filter(|&(_,f)| {
450+
!f.vis.is_accessible_from(self.curitem, &self.tcx.map)
451+
}).map(|(n,&_)|n).collect();
452+
453+
if !private_indexes.is_empty() {
454+
455+
let mut error = struct_span_err!(self.tcx.sess, expr.span, E0450,
456+
"cannot invoke tuple struct constructor \
457+
with private fields");
458+
error.span_label(expr.span,
459+
&format!("cannot construct with a private field"));
460+
461+
if let Some(def_id) = self.tcx.map.as_local_node_id(def.did) {
462+
if let Some(hir::map::NodeItem(node)) = self.tcx.map.find(def_id) {
463+
if let hir::Item_::ItemStruct(ref tuple_data, _) = node.node {
464+
465+
for i in private_indexes {
466+
error.span_label(tuple_data.fields()[i].span,
467+
&format!("private field declared here"));
468+
}
469+
}
470+
}
471+
}
472+
error.emit();
454473
}
455474
}
456475
}

src/test/compile-fail/E0450.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
// except according to those terms.
1010

1111
mod Bar {
12-
pub struct Foo(isize);
12+
pub struct Foo( bool, pub i32, f32, bool);
13+
//~^ NOTE private field declared here
14+
//~| NOTE private field declared here
15+
//~| NOTE private field declared here
1316
}
1417

1518
fn main() {
16-
let f = Bar::Foo(0); //~ ERROR E0450
19+
let f = Bar::Foo(false,1,0.1, true); //~ ERROR E0450
20+
//~^ NOTE cannot construct with a private field
1721
}

0 commit comments

Comments
 (0)