Skip to content

Commit

Permalink
don't ICE on cross-crate associated const type mismatch
Browse files Browse the repository at this point in the history
Fixes #41549.
  • Loading branch information
arielb1 committed Apr 27, 2017
1 parent 54ef800 commit 019a23e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/librustc_typeck/check/compare_method.rs
Expand Up @@ -788,16 +788,18 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait",
trait_c.name);

// Add a label to the Span containing just the type of the item
let trait_c_node_id = tcx.hir.as_local_node_id(trait_c.def_id).unwrap();
let trait_c_span = match tcx.hir.expect_trait_item(trait_c_node_id).node {
TraitItemKind::Const(ref ty, _) => ty.span,
_ => bug!("{:?} is not a trait const", trait_c),
};
let trait_c_node_id = tcx.hir.as_local_node_id(trait_c.def_id);
let trait_c_span = trait_c_node_id.map(|trait_c_node_id| {
// Add a label to the Span containing just the type of the const
match tcx.hir.expect_trait_item(trait_c_node_id).node {
TraitItemKind::Const(ref ty, _) => ty.span,
_ => bug!("{:?} is not a trait const", trait_c),
}
});

infcx.note_type_err(&mut diag,
&cause,
Some((trait_c_span, format!("type in trait"))),
trait_c_span.map(|span| (span, format!("type in trait"))),
Some(infer::ValuePairs::Types(ExpectedFound {
expected: trait_ty,
found: impl_ty,
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/auxiliary/issue_41549.rs
@@ -0,0 +1,15 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

pub trait Trait {
const CONST: u32;
}
23 changes: 23 additions & 0 deletions src/test/compile-fail/issue-41549.rs
@@ -0,0 +1,23 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue_41549.rs

#![feature(associated_consts)]

extern crate issue_41549;

struct S;

impl issue_41549::Trait for S {
const CONST: () = (); //~ ERROR incompatible type for trait
}

fn main() {}

0 comments on commit 019a23e

Please sign in to comment.