Skip to content

Commit d21c9b9

Browse files
authored
Rollup merge of #41583 - arielb1:cross-constant, r=eddyb
don't ICE on cross-crate associated const type mismatch Fixes #41549. r? @eddyb
2 parents b37fd3d + 019a23e commit d21c9b9

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/librustc_typeck/check/compare_method.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -788,16 +788,18 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
788788
trait",
789789
trait_c.name);
790790

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

798800
infcx.note_type_err(&mut diag,
799801
&cause,
800-
Some((trait_c_span, format!("type in trait"))),
802+
trait_c_span.map(|span| (span, format!("type in trait"))),
801803
Some(infer::ValuePairs::Types(ExpectedFound {
802804
expected: trait_ty,
803805
found: impl_ty,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(associated_consts)]
12+
13+
pub trait Trait {
14+
const CONST: u32;
15+
}

src/test/compile-fail/issue-41549.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue_41549.rs
12+
13+
#![feature(associated_consts)]
14+
15+
extern crate issue_41549;
16+
17+
struct S;
18+
19+
impl issue_41549::Trait for S {
20+
const CONST: () = (); //~ ERROR incompatible type for trait
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)