Skip to content

Commit fea0fd7

Browse files
dlrobertsonpietroalbini
authored andcommitted
typeck: Save the index of private fields
Save the index of all fields regardless of their visibility. Problems could occur later when attempting to index fields in error recovery if they are not inserted.
1 parent 352f32b commit fea0fd7

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3078,12 +3078,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30783078
if let Some(index) = fields.iter().position(|f| f.name.to_ident() == ident) {
30793079
let field = &fields[index];
30803080
let field_ty = self.field_ty(expr.span, field, substs);
3081+
// Save the index of all fields regardless of their visibility in case
3082+
// of error recovery.
3083+
self.write_field_index(expr.id, index);
30813084
if field.vis.is_accessible_from(def_scope, self.tcx) {
30823085
let adjustments = autoderef.adjust_steps(needs);
30833086
self.apply_adjustments(base, adjustments);
30843087
autoderef.finalize();
30853088

3086-
self.write_field_index(expr.id, index);
30873089
self.tcx.check_stability(field.did, Some(expr.id), expr.span);
30883090
return field_ty;
30893091
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// force-host
12+
// no-prefer-dynamic
13+
14+
#![feature(proc_macro, proc_macro_lib)]
15+
#![crate_type = "proc-macro"]
16+
17+
extern crate proc_macro;
18+
use proc_macro::TokenStream;
19+
20+
#[proc_macro_derive(Derive)]
21+
pub fn derive(_: TokenStream) -> TokenStream {
22+
let code = "
23+
fn one(r: Restricted) {
24+
r.field;
25+
}
26+
fn two(r: Restricted) {
27+
r.field;
28+
}
29+
";
30+
31+
code.parse().unwrap()
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue_50493.rs
12+
// ignore-stage1
13+
14+
#![feature(proc_macro)]
15+
16+
#[macro_use]
17+
extern crate issue_50493;
18+
19+
#[derive(Derive)] //~ ERROR field `field` of struct `Restricted` is private
20+
struct Restricted {
21+
pub(in restricted) field: usize, //~ visibilities can only be restricted to ancestor modules
22+
}
23+
24+
mod restricted {}
25+
26+
fn main() {}
27+

0 commit comments

Comments
 (0)