From 838595add0586e3e23967722e2005b66ea8387b8 Mon Sep 17 00:00:00 2001 From: Junseok Lee Date: Wed, 18 Feb 2015 16:23:13 -0800 Subject: [PATCH 1/4] suggestions no longer include private fields for structs outside local crate. closes #22421 --- src/librustc_typeck/check/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index e443b4d0e60..9af5b85993c 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -117,7 +117,7 @@ use std::iter::repeat; use std::slice; use syntax::{self, abi, attr}; use syntax::attr::AttrMetaMethods; -use syntax::ast::{self, ProvidedMethod, RequiredMethod, TypeTraitItem, DefId}; +use syntax::ast::{self, ProvidedMethod, RequiredMethod, TypeTraitItem, DefId, Visibility}; use syntax::ast_util::{self, local_def, PostExpansionMethod}; use syntax::codemap::{self, Span}; use syntax::owned_slice::OwnedSlice; @@ -3117,6 +3117,10 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, if skip.iter().any(|&x| x == n) { continue; } + // ignore private fields from non-local crates + if id.krate != ast::LOCAL_CRATE && elem.vis != Visibility::Public { + continue; + } let dist = lev_distance(n, name); if dist < best_dist { best = Some(n); From 85069e0529116d4f4e78bdcbd4f3c88274a678f0 Mon Sep 17 00:00:00 2001 From: Junseok Lee Date: Wed, 18 Feb 2015 17:53:56 -0800 Subject: [PATCH 2/4] added 'suggest-private-fields' cfail test --- src/test/compile-fail/suggest-private-fields.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/compile-fail/suggest-private-fields.rs diff --git a/src/test/compile-fail/suggest-private-fields.rs b/src/test/compile-fail/suggest-private-fields.rs new file mode 100644 index 00000000000..92191b19803 --- /dev/null +++ b/src/test/compile-fail/suggest-private-fields.rs @@ -0,0 +1,13 @@ +// aux-build:struct-field-privacy.rs + +extern crate "struct-field-privacy" as xc; + +use xc::B; + +fn main () { + let k = B { + aa: 20, //~ ERROR structure `struct-field-privacy::B` has no field named `aa` + //~^ HELP did you mean `a`? + bb: 20, //~ ERROR structure `struct-field-privacy::B` has no field named `bb` + }; +} From 3419d511991ae418ec55af594b303f37ffdc46c4 Mon Sep 17 00:00:00 2001 From: Junseok Lee Date: Wed, 18 Feb 2015 17:58:15 -0800 Subject: [PATCH 3/4] added local crate struct with priv field to test --- src/test/compile-fail/suggest-private-fields.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/compile-fail/suggest-private-fields.rs b/src/test/compile-fail/suggest-private-fields.rs index 92191b19803..167bdf791cc 100644 --- a/src/test/compile-fail/suggest-private-fields.rs +++ b/src/test/compile-fail/suggest-private-fields.rs @@ -4,10 +4,23 @@ extern crate "struct-field-privacy" as xc; use xc::B; +struct A { + pub a: u32, + b: u32, +} + fn main () { + // external crate struct let k = B { aa: 20, //~ ERROR structure `struct-field-privacy::B` has no field named `aa` //~^ HELP did you mean `a`? bb: 20, //~ ERROR structure `struct-field-privacy::B` has no field named `bb` }; + // local crate struct + let l = A { + aa: 20, //~ ERROR structure `A` has no field named `aa` + //~^ HELP did you mean `a`? + bb: 20, //~ ERROR structure `A` has no field named `bb` + //~^ HELP did you mean `b`? + }; } From 9b67d07acc7573276a15dfcd9e4418bc3d838074 Mon Sep 17 00:00:00 2001 From: Junseok Lee Date: Wed, 18 Feb 2015 17:59:44 -0800 Subject: [PATCH 4/4] added license header to test --- src/test/compile-fail/suggest-private-fields.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/compile-fail/suggest-private-fields.rs b/src/test/compile-fail/suggest-private-fields.rs index 167bdf791cc..30648498ba8 100644 --- a/src/test/compile-fail/suggest-private-fields.rs +++ b/src/test/compile-fail/suggest-private-fields.rs @@ -1,3 +1,13 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + // aux-build:struct-field-privacy.rs extern crate "struct-field-privacy" as xc;