Skip to content

Commit ac5c084

Browse files
committed
Auto merge of #50693 - dlrobertson:fix_50493, r=petrochenkov
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. Fixes: #50493
2 parents 935a2f1 + cdd6139 commit ac5c084

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/librustc_typeck/check/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3081,12 +3081,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30813081
if let Some(index) = fields.iter().position(|f| f.name.to_ident() == ident) {
30823082
let field = &fields[index];
30833083
let field_ty = self.field_ty(expr.span, field, substs);
3084+
// Save the index of all fields regardless of their visibility in case
3085+
// of error recovery.
3086+
self.write_field_index(expr.id, index);
30843087
if field.vis.is_accessible_from(def_scope, self.tcx) {
30853088
let adjustments = autoderef.adjust_steps(needs);
30863089
self.apply_adjustments(base, adjustments);
30873090
autoderef.finalize();
30883091

3089-
self.write_field_index(expr.id, index);
30903092
self.tcx.check_stability(field.did, Some(expr.id), expr.span);
30913093
return field_ty;
30923094
}
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+
}
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)