Skip to content

Lower the priority of unstable methods when picking a candidate. #48552

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

Merged
merged 6 commits into from
Mar 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Track IsSuggestion in ProbeContext. Don't warn stability for suggesti…
…ons.
  • Loading branch information
kennytm committed Mar 23, 2018
commit 17cc3d77d16e0cc3dfa1b3ee9749e2fca3ebb9e7
22 changes: 13 additions & 9 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use self::PickKind::*;

/// Boolean flag used to indicate if this search is for a suggestion
/// or not. If true, we can allow ambiguity and so forth.
#[derive(Clone, Copy)]
pub struct IsSuggestion(pub bool);

struct ProbeContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
Expand Down Expand Up @@ -66,6 +67,8 @@ struct ProbeContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
/// Collects near misses when trait bounds for type parameters are unsatisfied and is only used
/// for error reporting
unsatisfied_predicates: Vec<TraitRef<'tcx>>,

is_suggestion: IsSuggestion,
}

impl<'a, 'gcx, 'tcx> Deref for ProbeContext<'a, 'gcx, 'tcx> {
Expand Down Expand Up @@ -277,8 +280,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// this creates one big transaction so that all type variables etc
// that we create during the probe process are removed later
self.probe(|_| {
let mut probe_cx =
ProbeContext::new(self, span, mode, method_name, return_type, Rc::new(steps));
let mut probe_cx = ProbeContext::new(
self, span, mode, method_name, return_type, Rc::new(steps), is_suggestion,
);

probe_cx.assemble_inherent_candidates();
match scope {
Expand Down Expand Up @@ -379,7 +383,8 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
mode: Mode,
method_name: Option<ast::Name>,
return_type: Option<Ty<'tcx>>,
steps: Rc<Vec<CandidateStep<'tcx>>>)
steps: Rc<Vec<CandidateStep<'tcx>>>,
is_suggestion: IsSuggestion)
-> ProbeContext<'a, 'gcx, 'tcx> {
ProbeContext {
fcx,
Expand All @@ -395,6 +400,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
allow_similar_names: false,
private_candidate: None,
unsatisfied_predicates: Vec::new(),
is_suggestion,
}
}

Expand Down Expand Up @@ -952,14 +958,12 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
Some(&mut unstable_candidates),
);
if let Some(pick) = res {
if !unstable_candidates.is_empty() && !self_ty.is_ty_var() {
if !self.is_suggestion.0 && !unstable_candidates.is_empty() {
if let Ok(p) = &pick {
// Emit a lint if there are unstable candidates alongside the stable ones.
//
// Note, we suppress warning if `self_ty` is TyVar (`_`), since every
// possible candidates of every type will be considered, which leads to
// bogus ambiguity like `str::rsplit` vs `[_]::rsplit`. This condition is
// seen in `src/test/compile-fail/occurs-check-2.rs`.
// We suppress warning if we're picking the method only because it is a
// suggestion.
self.emit_unstable_name_collision_hint(p, &unstable_candidates);
}
}
Expand Down Expand Up @@ -1265,7 +1269,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
let steps = self.steps.clone();
self.probe(|_| {
let mut pcx = ProbeContext::new(self.fcx, self.span, self.mode, self.method_name,
self.return_type, steps);
self.return_type, steps, IsSuggestion(true));
pcx.allow_similar_names = true;
pcx.assemble_inherent_candidates();
pcx.assemble_extension_candidates_for_traits_in_scope(ast::DUMMY_NODE_ID)?;
Expand Down