-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Winnow private method candidates instead of assuming any candidate of the right name will apply #125622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Winnow private method candidates instead of assuming any candidate of the right name will apply #125622
Changes from all commits
5e8df95
14f9c63
7d151fa
7894a11
8189506
ffb1b2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ use rustc_trait_selection::traits::query::method_autoderef::{ | |
use rustc_trait_selection::traits::query::CanonicalTyGoal; | ||
use rustc_trait_selection::traits::ObligationCtxt; | ||
use rustc_trait_selection::traits::{self, ObligationCause}; | ||
use std::cell::Cell; | ||
use std::cell::RefCell; | ||
use std::cmp::max; | ||
use std::iter; | ||
|
@@ -76,8 +77,12 @@ pub(crate) struct ProbeContext<'a, 'tcx> { | |
/// requested name (by edit distance) | ||
allow_similar_names: bool, | ||
|
||
/// List of potential private candidates. Will be trimmed to ones that | ||
/// actually apply and then the result inserted into `private_candidate` | ||
private_candidates: Vec<Candidate<'tcx>>, | ||
|
||
/// Some(candidate) if there is a private candidate | ||
private_candidate: Option<(DefKind, DefId)>, | ||
private_candidate: Cell<Option<(DefKind, DefId)>>, | ||
Comment on lines
+80
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between these lol There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or actually, better question: Why do we need two fields here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the Of course we could just eagerly report (not return) an error from method selection and return that no methods were found, which will subsequently return an error containing the list of traits that should be imported. But then method selection is reporting errors, which is also not be desirable (and probably wrong in various cases where we'll return a better error later). |
||
|
||
/// Collects near misses when the candidate functions are missing a `self` keyword and is only | ||
/// used for error reporting | ||
|
@@ -581,7 +586,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { | |
orig_steps_var_values, | ||
steps, | ||
allow_similar_names: false, | ||
private_candidate: None, | ||
private_candidates: Vec::new(), | ||
private_candidate: Cell::new(None), | ||
static_candidates: RefCell::new(Vec::new()), | ||
unsatisfied_predicates: RefCell::new(Vec::new()), | ||
scope_expr_id, | ||
|
@@ -593,7 +599,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { | |
self.inherent_candidates.clear(); | ||
self.extension_candidates.clear(); | ||
self.impl_dups.clear(); | ||
self.private_candidate = None; | ||
self.private_candidates.clear(); | ||
self.private_candidate.set(None); | ||
self.static_candidates.borrow_mut().clear(); | ||
self.unsatisfied_predicates.borrow_mut().clear(); | ||
} | ||
|
@@ -617,9 +624,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { | |
} else { | ||
self.extension_candidates.push(candidate); | ||
} | ||
} else if self.private_candidate.is_none() { | ||
self.private_candidate = | ||
Some((candidate.item.kind.as_def_kind(), candidate.item.def_id)); | ||
} else { | ||
self.private_candidates.push(candidate); | ||
} | ||
} | ||
|
||
|
@@ -1171,7 +1177,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { | |
let mut possibly_unsatisfied_predicates = Vec::new(); | ||
|
||
for (kind, candidates) in | ||
&[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)] | ||
[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)] | ||
{ | ||
debug!("searching {} candidates", kind); | ||
let res = self.consider_candidates( | ||
|
@@ -1185,6 +1191,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { | |
} | ||
} | ||
|
||
if self.private_candidate.get().is_none() { | ||
if let Some(Ok(pick)) = | ||
self.consider_candidates(self_ty, &self.private_candidates, &mut vec![], None) | ||
{ | ||
self.private_candidate.set(Some((pick.item.kind.as_def_kind(), pick.item.def_id))); | ||
} | ||
} | ||
|
||
// `pick_method` may be called twice for the same self_ty if no stable methods | ||
// match. Only extend once. | ||
if unstable_candidates.is_some() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0599]: no function or associated item named `foo` found for struct `Foo<B>` in the current scope | ||
--> $DIR/ufc-method-call.rs:27:27 | ||
| | ||
LL | pub struct Foo<T>(T); | ||
| ----------------- function or associated item `foo` not found for this struct | ||
... | ||
LL | test::Foo::<test::B>::foo(); | ||
| ^^^ function or associated item not found in `Foo<B>` | ||
| | ||
= note: the function or associated item was found for | ||
- `Foo<A>` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! This test used to report that the method call cannot | ||
//! call the private method `Foo<A>::foo`, even though the user | ||
//! explicitly selected `Foo<B>::foo`. This is because we only | ||
//! looked for methods of the right name, without properly checking | ||
//! the `Self` type | ||
|
||
//@ revisions: same_name different_name | ||
|
||
pub mod test { | ||
pub struct A; | ||
pub struct B; | ||
pub struct Foo<T>(T); | ||
|
||
impl Foo<A> { | ||
fn foo() {} | ||
} | ||
|
||
impl Foo<B> { | ||
#[cfg(same_name)] | ||
fn foo() {} | ||
#[cfg(different_name)] | ||
fn bar() {} | ||
} | ||
} | ||
|
||
fn main() { | ||
test::Foo::<test::B>::foo(); | ||
//[same_name]~^ ERROR associated function `foo` is private | ||
//[different_name]~^^ ERROR no function or associated item named `foo` found for struct `Foo<B>` | ||
} |