From 7b837e075a335d482c583b574ebaf5006738655e Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Tue, 29 Aug 2023 13:41:33 -0700 Subject: [PATCH 1/2] Don't suggest adding parentheses to call an inaccessible method. Previously, the test code would emit E0615, thus revealing the existence of private methods that the programmer probably does not care about. Now it ignores their existence instead, producing error E0609 (no field). The motivating example is: ```rust let x = std::rc::Rc::new(()); x.inner; ``` which would previously mention the private method `Rc::inner()`, even though `Rc` intentionally has no public methods so that it can be a transparent smart pointer for any `T`. --- compiler/rustc_hir_typeck/src/expr.rs | 2 +- tests/ui/error-codes/E0609-private-method.rs | 16 ++++++++++++++++ tests/ui/error-codes/E0609-private-method.stderr | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/ui/error-codes/E0609-private-method.rs create mode 100644 tests/ui/error-codes/E0609-private-method.stderr diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 7cea40fdd64c2..72c58bf1c2d6e 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -2314,7 +2314,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { field, base_ty, expr.hir_id, - true, + false, // don't mention or suggest non-accessible methods expected.only_has_type(self), ) { self.ban_take_value_of_method(expr, base_ty, field) diff --git a/tests/ui/error-codes/E0609-private-method.rs b/tests/ui/error-codes/E0609-private-method.rs new file mode 100644 index 0000000000000..dfa97ad9a6f40 --- /dev/null +++ b/tests/ui/error-codes/E0609-private-method.rs @@ -0,0 +1,16 @@ +// This error is an E0609 and *not* an E0615 because the fact that the method exists is not +// relevant. +mod foo { + pub struct Foo { + x: u32, + } + + impl Foo { + fn method(&self) {} + } +} + +fn main() { + let f = foo::Foo { x: 0 }; + f.method; //~ ERROR E0609 +} diff --git a/tests/ui/error-codes/E0609-private-method.stderr b/tests/ui/error-codes/E0609-private-method.stderr new file mode 100644 index 0000000000000..d2a11e9062740 --- /dev/null +++ b/tests/ui/error-codes/E0609-private-method.stderr @@ -0,0 +1,9 @@ +error[E0609]: no field `method` on type `Foo` + --> $DIR/E0609-private-method.rs:15:7 + | +LL | f.method; + | ^^^^^^ unknown field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0609`. From 4e9a2a6ff64371a4141c106973a748cfe9625f3d Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Tue, 29 Aug 2023 15:33:12 -0700 Subject: [PATCH 2/2] Remove `allow_private` entirely. --- compiler/rustc_hir_typeck/src/expr.rs | 10 ++-------- compiler/rustc_hir_typeck/src/method/mod.rs | 5 ++--- compiler/rustc_hir_typeck/src/method/suggest.rs | 3 +-- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 72c58bf1c2d6e..a3b8c391e023b 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -2310,13 +2310,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let guar = if field.name == kw::Empty { self.tcx.sess.delay_span_bug(field.span, "field name with no name") - } else if self.method_exists( - field, - base_ty, - expr.hir_id, - false, // don't mention or suggest non-accessible methods - expected.only_has_type(self), - ) { + } else if self.method_exists(field, base_ty, expr.hir_id, expected.only_has_type(self)) { self.ban_take_value_of_method(expr, base_ty, field) } else if !base_ty.is_primitive_ty() { self.ban_nonexisting_field(field, base, expr, base_ty) @@ -2501,7 +2495,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let mut err = self.private_field_err(field, base_did); // Also check if an accessible method exists, which is often what is meant. - if self.method_exists(field, expr_t, expr.hir_id, false, return_ty) + if self.method_exists(field, expr_t, expr.hir_id, return_ty) && !self.expr_in_place(expr.hir_id) { self.suggest_method_call( diff --git a/compiler/rustc_hir_typeck/src/method/mod.rs b/compiler/rustc_hir_typeck/src/method/mod.rs index 6dd131aa28397..86a0e95de1dab 100644 --- a/compiler/rustc_hir_typeck/src/method/mod.rs +++ b/compiler/rustc_hir_typeck/src/method/mod.rs @@ -89,14 +89,13 @@ pub enum CandidateSource { } impl<'a, 'tcx> FnCtxt<'a, 'tcx> { - /// Determines whether the type `self_ty` supports a method name `method_name` or not. + /// Determines whether the type `self_ty` supports a visible method named `method_name` or not. #[instrument(level = "debug", skip(self))] pub fn method_exists( &self, method_name: Ident, self_ty: Ty<'tcx>, call_expr_id: hir::HirId, - allow_private: bool, return_type: Option>, ) -> bool { match self.probe_for_name( @@ -118,7 +117,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } Err(NoMatch(..)) => false, Err(Ambiguity(..)) => true, - Err(PrivateMatch(..)) => allow_private, + Err(PrivateMatch(..)) => false, Err(IllegalSizedBound { .. }) => true, Err(BadReturnType) => false, } diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 72a04a02bf4fb..07c48ec6392a8 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -2361,8 +2361,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some(output_ty) => self.resolve_vars_if_possible(output_ty), _ => return, }; - let method_exists = - self.method_exists(item_name, output_ty, call.hir_id, true, return_type); + let method_exists = self.method_exists(item_name, output_ty, call.hir_id, return_type); debug!("suggest_await_before_method: is_method_exist={}", method_exists); if method_exists { err.span_suggestion_verbose(