Skip to content

Commit

Permalink
Rollup merge of rust-lang#35375 - trixnz:update-error-326, r=jonathan…
Browse files Browse the repository at this point in the history
…dturner

Update error format for E0326

Fixes rust-lang#35335 as part of rust-lang#35233

r? @jonathandturner
  • Loading branch information
Jonathan Turner committed Aug 11, 2016
2 parents 0fecdc5 + 034df94 commit e24c326
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 23 additions & 1 deletion src/librustc_typeck/check/compare_method.rs
Expand Up @@ -14,6 +14,8 @@ use rustc::ty;
use rustc::traits::{self, ProjectionMode};
use rustc::ty::error::ExpectedFound;
use rustc::ty::subst::{self, Subst, Substs, VecPerParamSpace};
use rustc::hir::map::Node;
use rustc::hir::{ImplItemKind, TraitItem_};

use syntax::ast;
use syntax_pos::Span;
Expand Down Expand Up @@ -461,7 +463,7 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
// Compute skolemized form of impl and trait const tys.
let impl_ty = impl_c.ty.subst(tcx, impl_to_skol_substs);
let trait_ty = trait_c.ty.subst(tcx, &trait_to_skol_substs);
let origin = TypeOrigin::Misc(impl_c_span);
let mut origin = TypeOrigin::Misc(impl_c_span);

let err = infcx.commit_if_ok(|_| {
// There is no "body" here, so just pass dummy id.
Expand Down Expand Up @@ -496,11 +498,31 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
debug!("checking associated const for compatibility: impl ty {:?}, trait ty {:?}",
impl_ty,
trait_ty);

// Locate the Span containing just the type of the offending impl
if let Some(impl_trait_node) = tcx.map.get_if_local(impl_c.def_id) {
if let Node::NodeImplItem(impl_trait_item) = impl_trait_node {
if let ImplItemKind::Const(ref ty, _) = impl_trait_item.node {
origin = TypeOrigin::Misc(ty.span);
}
}
}

let mut diag = struct_span_err!(
tcx.sess, origin.span(), E0326,
"implemented const `{}` has an incompatible type for trait",
trait_c.name
);

// Add a label to the Span containing just the type of the item
if let Some(orig_trait_node) = tcx.map.get_if_local(trait_c.def_id) {
if let Node::NodeTraitItem(orig_trait_item) = orig_trait_node {
if let TraitItem_::ConstTraitItem(ref ty, _) = orig_trait_item.node {
diag.span_label(ty.span, &format!("original trait requirement"));
}
}
}

infcx.note_type_err(
&mut diag, origin,
Some(infer::ValuePairs::Types(ExpectedFound {
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/associated-const-impl-wrong-type.rs
Expand Up @@ -11,15 +11,15 @@
#![feature(associated_consts)]

trait Foo {
const BAR: u32;
const BAR: u32; //~ NOTE original trait requirement
}

struct SignedBar;

impl Foo for SignedBar {
const BAR: i32 = -1;
//~^ ERROR implemented const `BAR` has an incompatible type for trait [E0326]
//~| expected u32, found i32
//~| NOTE expected u32, found i32
}

fn main() {}

0 comments on commit e24c326

Please sign in to comment.