Skip to content

Commit

Permalink
Auto merge of #25065 - quantheory:fix_associated_const_ambiguity_mess…
Browse files Browse the repository at this point in the history
…age, r=nikomatsakis

This fixes #24922 and #25017, and reduces the number of error messages that talk about "methods" when associated constants rather than methods are involved.

I will admit that I haven't thought very carefully about the error messages. My goal has been to make more of the messages technically correct in all situations, and to avoid ICEs. But in some cases we could probably talk specifically about "methods" rather than "items".
  • Loading branch information
bors committed May 14, 2015
2 parents e539424 + b4bbf3a commit 1b5b639
Show file tree
Hide file tree
Showing 36 changed files with 191 additions and 150 deletions.
7 changes: 5 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2528,15 +2528,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// If anything ends up here entirely resolved,
// it's an error. If anything ends up here
// partially resolved, that's OK, because it may
// be a `T::CONST` that typeck will resolve to
// an inherent impl.
// be a `T::CONST` that typeck will resolve.
if path_res.depth == 0 {
self.resolve_error(
path.span,
&format!("`{}` is not an enum variant, struct or const",
token::get_ident(
path.segments.last().unwrap().identifier)));
} else {
let const_name = path.segments.last().unwrap()
.identifier.name;
let traits = self.get_traits_containing_item(const_name);
self.trait_map.insert(pattern.id, traits);
self.record_def(pattern.id, path_res);
}
}
Expand Down
73 changes: 36 additions & 37 deletions src/librustc_typeck/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ use middle::def;
use middle::privacy::{AllPublic, DependsOn, LastPrivate, LastMod};
use middle::subst;
use middle::traits;
use middle::ty::*;
use middle::ty;
use middle::ty::{self, AsPredicate, ToPolyTraitRef};
use middle::infer;
use util::ppaux::Repr;

use std::rc::Rc;
use syntax::ast::DefId;
use syntax::ast;
use syntax::codemap::Span;
Expand All @@ -39,7 +37,7 @@ pub enum MethodError {
// Did not find an applicable method, but we did find various
// static methods that may apply, as well as a list of
// not-in-scope traits which may work.
NoMatch(Vec<CandidateSource>, Vec<ast::DefId>),
NoMatch(Vec<CandidateSource>, Vec<ast::DefId>, probe::Mode),

// Multiple methods might apply.
Ambiguity(Vec<CandidateSource>),
Expand All @@ -62,7 +60,7 @@ type ItemIndex = usize; // just for doc purposes
pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span: Span,
method_name: ast::Name,
self_ty: Ty<'tcx>,
self_ty: ty::Ty<'tcx>,
call_expr_id: ast::NodeId)
-> bool
{
Expand Down Expand Up @@ -92,11 +90,11 @@ pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
pub fn lookup<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span: Span,
method_name: ast::Name,
self_ty: Ty<'tcx>,
supplied_method_types: Vec<Ty<'tcx>>,
self_ty: ty::Ty<'tcx>,
supplied_method_types: Vec<ty::Ty<'tcx>>,
call_expr: &'tcx ast::Expr,
self_expr: &'tcx ast::Expr)
-> Result<MethodCallee<'tcx>, MethodError>
-> Result<ty::MethodCallee<'tcx>, MethodError>
{
debug!("lookup(method_name={}, self_ty={}, call_expr={}, self_expr={})",
method_name.repr(fcx.tcx()),
Expand All @@ -115,9 +113,9 @@ pub fn lookup_in_trait<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
self_expr: Option<&ast::Expr>,
m_name: ast::Name,
trait_def_id: DefId,
self_ty: Ty<'tcx>,
opt_input_types: Option<Vec<Ty<'tcx>>>)
-> Option<MethodCallee<'tcx>>
self_ty: ty::Ty<'tcx>,
opt_input_types: Option<Vec<ty::Ty<'tcx>>>)
-> Option<ty::MethodCallee<'tcx>>
{
lookup_in_trait_adjusted(fcx, span, self_expr, m_name, trait_def_id,
0, false, self_ty, opt_input_types)
Expand All @@ -139,9 +137,9 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
trait_def_id: DefId,
autoderefs: usize,
unsize: bool,
self_ty: Ty<'tcx>,
opt_input_types: Option<Vec<Ty<'tcx>>>)
-> Option<MethodCallee<'tcx>>
self_ty: ty::Ty<'tcx>,
opt_input_types: Option<Vec<ty::Ty<'tcx>>>)
-> Option<ty::MethodCallee<'tcx>>
{
debug!("lookup_in_trait_adjusted(self_ty={}, self_expr={}, m_name={}, trait_def_id={})",
self_ty.repr(fcx.tcx()),
Expand Down Expand Up @@ -186,7 +184,9 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
// Trait must have a method named `m_name` and it should not have
// type parameters or early-bound regions.
let tcx = fcx.tcx();
let (method_num, method_ty) = trait_method(tcx, trait_def_id, m_name).unwrap();
let (method_num, method_ty) = trait_item(tcx, trait_def_id, m_name)
.and_then(|(idx, item)| item.as_opt_method().map(|m| (idx, m)))
.unwrap();
assert_eq!(method_ty.generics.types.len(subst::FnSpace), 0);
assert_eq!(method_ty.generics.regions.len(subst::FnSpace), 0);

Expand Down Expand Up @@ -288,10 +288,10 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
}
}

let callee = MethodCallee {
origin: MethodTypeParam(MethodParam{trait_ref: trait_ref.clone(),
method_num: method_num,
impl_def_id: None}),
let callee = ty::MethodCallee {
origin: ty::MethodTypeParam(ty::MethodParam{trait_ref: trait_ref.clone(),
method_num: method_num,
impl_def_id: None}),
ty: fty,
substs: trait_ref.substs.clone()
};
Expand All @@ -304,7 +304,7 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
pub fn resolve_ufcs<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span: Span,
method_name: ast::Name,
self_ty: Ty<'tcx>,
self_ty: ty::Ty<'tcx>,
expr_id: ast::NodeId)
-> Result<(def::Def, LastPrivate), MethodError>
{
Expand All @@ -322,41 +322,40 @@ pub fn resolve_ufcs<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
_ => def::FromTrait(pick.item.container().id())
};
let def_result = match pick.item {
ImplOrTraitItem::MethodTraitItem(..) => def::DefMethod(def_id, provenance),
ImplOrTraitItem::ConstTraitItem(..) => def::DefAssociatedConst(def_id, provenance),
ImplOrTraitItem::TypeTraitItem(..) => {
ty::ImplOrTraitItem::MethodTraitItem(..) => def::DefMethod(def_id, provenance),
ty::ImplOrTraitItem::ConstTraitItem(..) => def::DefAssociatedConst(def_id, provenance),
ty::ImplOrTraitItem::TypeTraitItem(..) => {
fcx.tcx().sess.span_bug(span, "resolve_ufcs: probe picked associated type");
}
};
Ok((def_result, lp))
}


/// Find method with name `method_name` defined in `trait_def_id` and return it, along with its
/// index (or `None`, if no such method).
fn trait_method<'tcx>(tcx: &ty::ctxt<'tcx>,
trait_def_id: ast::DefId,
method_name: ast::Name)
-> Option<(usize, Rc<ty::Method<'tcx>>)>
/// Find item with name `item_name` defined in `trait_def_id` and return it, along with its
/// index (or `None`, if no such item).
fn trait_item<'tcx>(tcx: &ty::ctxt<'tcx>,
trait_def_id: ast::DefId,
item_name: ast::Name)
-> Option<(usize, ty::ImplOrTraitItem<'tcx>)>
{
let trait_items = ty::trait_items(tcx, trait_def_id);
trait_items
.iter()
.enumerate()
.find(|&(_, ref item)| item.name() == method_name)
.and_then(|(idx, item)| item.as_opt_method().map(|m| (idx, m)))
.find(|&(_, ref item)| item.name() == item_name)
.map(|(num, item)| (num, (*item).clone()))
}

fn impl_method<'tcx>(tcx: &ty::ctxt<'tcx>,
impl_def_id: ast::DefId,
method_name: ast::Name)
-> Option<Rc<ty::Method<'tcx>>>
fn impl_item<'tcx>(tcx: &ty::ctxt<'tcx>,
impl_def_id: ast::DefId,
item_name: ast::Name)
-> Option<ty::ImplOrTraitItem<'tcx>>
{
let impl_items = tcx.impl_items.borrow();
let impl_items = impl_items.get(&impl_def_id).unwrap();
impl_items
.iter()
.map(|&did| ty::impl_or_trait_item(tcx, did.def_id()))
.find(|m| m.name() == method_name)
.and_then(|item| item.as_opt_method())
.find(|m| m.name() == item_name)
}
6 changes: 3 additions & 3 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn probe<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
let steps = if mode == Mode::MethodCall {
match create_steps(fcx, span, self_ty) {
Some(steps) => steps,
None => return Err(MethodError::NoMatch(Vec::new(), Vec::new())),
None => return Err(MethodError::NoMatch(Vec::new(), Vec::new(), mode)),
}
} else {
vec![CandidateStep {
Expand Down Expand Up @@ -866,7 +866,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
}
}
}).collect(),
Some(Err(MethodError::NoMatch(_, others))) => {
Some(Err(MethodError::NoMatch(_, others, _))) => {
assert!(others.is_empty());
vec![]
}
Expand All @@ -877,7 +877,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
None => vec![],
};

Err(MethodError::NoMatch(static_candidates, out_of_scope_traits))
Err(MethodError::NoMatch(static_candidates, out_of_scope_traits, self.mode))
}

fn pick_core(&mut self) -> Option<PickResult<'tcx>> {
Expand Down
Loading

0 comments on commit 1b5b639

Please sign in to comment.