Skip to content

Commit c658660

Browse files
author
mejrs
committed
Incorporate feedback
1 parent 14e9893 commit c658660

File tree

5 files changed

+75
-89
lines changed

5 files changed

+75
-89
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+36-79
Original file line numberDiff line numberDiff line change
@@ -1656,82 +1656,39 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16561656
};
16571657

16581658
enum Similar<'tcx> {
1659-
Adts(ty::AdtDef<'tcx>, ty::AdtDef<'tcx>),
1660-
PrimitiveFound(Ty<'tcx>, ty::AdtDef<'tcx>),
1661-
PrimitiveExpected(ty::AdtDef<'tcx>, Ty<'tcx>),
1662-
}
1663-
1664-
let primitive_sym = |kind: &_| match kind {
1665-
ty::Bool => Some(sym::bool),
1666-
ty::Char => Some(sym::char),
1667-
ty::Float(f) => match f {
1668-
ty::FloatTy::F32 => Some(sym::f32),
1669-
ty::FloatTy::F64 => Some(sym::f64),
1670-
},
1671-
ty::Int(f) => match f {
1672-
ty::IntTy::Isize => Some(sym::isize),
1673-
ty::IntTy::I8 => Some(sym::i8),
1674-
ty::IntTy::I16 => Some(sym::i16),
1675-
ty::IntTy::I32 => Some(sym::i32),
1676-
ty::IntTy::I64 => Some(sym::i64),
1677-
ty::IntTy::I128 => Some(sym::i128),
1678-
},
1679-
ty::Uint(f) => match f {
1680-
ty::UintTy::Usize => Some(sym::usize),
1681-
ty::UintTy::U8 => Some(sym::u8),
1682-
ty::UintTy::U16 => Some(sym::u16),
1683-
ty::UintTy::U32 => Some(sym::u32),
1684-
ty::UintTy::U64 => Some(sym::u64),
1685-
ty::UintTy::U128 => Some(sym::u128),
1686-
},
1687-
_ => None,
1688-
};
1689-
1690-
let similarity = |e: ExpectedFound<Ty<'tcx>>| {
1691-
let (fk, ek) = (e.found.kind(), e.expected.kind());
1692-
match (fk, ek) {
1693-
(
1694-
ty::Adt(adt, _),
1695-
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_),
1696-
) => {
1697-
let path = self.tcx.def_path(adt.did()).data;
1698-
let name = path.last().unwrap().data.get_opt_name();
1699-
let prim_sym = primitive_sym(ek);
1659+
Adts { expected: ty::AdtDef<'tcx>, found: ty::AdtDef<'tcx> },
1660+
PrimitiveFound { expected: ty::AdtDef<'tcx>, found: Ty<'tcx> },
1661+
PrimitiveExpected { expected: Ty<'tcx>, found: ty::AdtDef<'tcx> },
1662+
}
17001663

1701-
if name == prim_sym {
1702-
return Some(Similar::PrimitiveExpected(*adt, e.expected));
1703-
}
1704-
None
1664+
let similarity = |ExpectedFound { expected, found }: ExpectedFound<Ty<'tcx>>| {
1665+
if let ty::Adt(expected, _) = expected.kind() && let Some(primitive) = found.primitive_symbol() {
1666+
let path = self.tcx.def_path(expected.did()).data;
1667+
let name = path.last().unwrap().data.get_opt_name();
1668+
if name == Some(primitive) {
1669+
return Some(Similar::PrimitiveFound { expected: *expected, found });
17051670
}
1706-
(
1707-
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_),
1708-
ty::Adt(adt, _),
1709-
) => {
1710-
let path = self.tcx.def_path(adt.did()).data;
1711-
let name = path.last().unwrap().data.get_opt_name();
1712-
let prim_sym = primitive_sym(fk);
1713-
1714-
if name == prim_sym {
1715-
return Some(Similar::PrimitiveFound(e.expected, *adt));
1716-
}
1717-
None
1671+
} else if let Some(primitive) = expected.primitive_symbol() && let ty::Adt(found, _) = found.kind() {
1672+
let path = self.tcx.def_path(found.did()).data;
1673+
let name = path.last().unwrap().data.get_opt_name();
1674+
if name == Some(primitive) {
1675+
return Some(Similar::PrimitiveExpected { expected, found: *found });
17181676
}
1719-
(ty::Adt(f, _), ty::Adt(e, _)) => {
1720-
if !f.did().is_local() && f.did().krate == e.did().krate {
1721-
// Most likely types from different versions of the same crate
1722-
// are in play, in which case this message isn't so helpful.
1723-
// A "perhaps two different versions..." error is already emitted for that.
1724-
return None;
1725-
}
1726-
let e_path = self.tcx.def_path(e.did()).data;
1727-
let f_path = self.tcx.def_path(f.did()).data;
1728-
if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last()) && e_last == f_last {
1729-
return Some(Similar::Adts(*f, *e));
1730-
}
1731-
None
1677+
} else if let ty::Adt(expected, _) = expected.kind() && let ty::Adt(found, _) = found.kind() {
1678+
if !expected.did().is_local() && expected.did().krate == found.did().krate {
1679+
// Most likely types from different versions of the same crate
1680+
// are in play, in which case this message isn't so helpful.
1681+
// A "perhaps two different versions..." error is already emitted for that.
1682+
return None;
1683+
}
1684+
let f_path = self.tcx.def_path(found.did()).data;
1685+
let e_path = self.tcx.def_path(expected.did()).data;
1686+
1687+
if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last()) && e_last == f_last {
1688+
return Some(Similar::Adts{expected: *expected, found: *found});
17321689
}
1733-
_ => None,
17341690
}
1691+
None
17351692
};
17361693

17371694
match terr {
@@ -1759,8 +1716,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17591716
};
17601717

17611718
let diagnose_adts =
1762-
|found_adt: ty::AdtDef<'tcx>,
1763-
expected_adt: ty::AdtDef<'tcx>,
1719+
|expected_adt : ty::AdtDef<'tcx>,
1720+
found_adt: ty::AdtDef<'tcx>,
17641721
diagnostic: &mut Diagnostic| {
17651722
let found_name = values.found.sort_string(self.tcx);
17661723
let expected_name = values.expected.sort_string(self.tcx);
@@ -1792,14 +1749,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17921749
};
17931750

17941751
match s {
1795-
Similar::Adts(found_adt, expected_adt) => {
1796-
diagnose_adts(found_adt, expected_adt, diag)
1752+
Similar::Adts{expected, found} => {
1753+
diagnose_adts(expected, found, diag)
17971754
}
1798-
Similar::PrimitiveFound(prim, e) => {
1799-
diagnose_primitive(prim, values.expected, e.did(), diag)
1755+
Similar::PrimitiveFound{expected, found: prim} => {
1756+
diagnose_primitive(prim, values.expected, expected.did(), diag)
18001757
}
1801-
Similar::PrimitiveExpected(f, prim) => {
1802-
diagnose_primitive(prim, values.found, f.did(), diag)
1758+
Similar::PrimitiveExpected{expected: prim, found} => {
1759+
diagnose_primitive(prim, values.found, found.did(), diag)
18031760
}
18041761
}
18051762
}

compiler/rustc_middle/src/ty/sty.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hir as hir;
1919
use rustc_hir::def_id::DefId;
2020
use rustc_index::vec::Idx;
2121
use rustc_macros::HashStable;
22-
use rustc_span::symbol::{kw, Symbol};
22+
use rustc_span::symbol::{kw, sym, Symbol};
2323
use rustc_target::abi::VariantIdx;
2424
use rustc_target::spec::abi;
2525
use std::borrow::Cow;
@@ -2274,6 +2274,35 @@ impl<'tcx> Ty<'tcx> {
22742274
}
22752275
}
22762276
}
2277+
2278+
// If `self` is a primitive, return its [`Symbol`].
2279+
pub fn primitive_symbol(self) -> Option<Symbol> {
2280+
match self.kind() {
2281+
ty::Bool => Some(sym::bool),
2282+
ty::Char => Some(sym::char),
2283+
ty::Float(f) => match f {
2284+
ty::FloatTy::F32 => Some(sym::f32),
2285+
ty::FloatTy::F64 => Some(sym::f64),
2286+
},
2287+
ty::Int(f) => match f {
2288+
ty::IntTy::Isize => Some(sym::isize),
2289+
ty::IntTy::I8 => Some(sym::i8),
2290+
ty::IntTy::I16 => Some(sym::i16),
2291+
ty::IntTy::I32 => Some(sym::i32),
2292+
ty::IntTy::I64 => Some(sym::i64),
2293+
ty::IntTy::I128 => Some(sym::i128),
2294+
},
2295+
ty::Uint(f) => match f {
2296+
ty::UintTy::Usize => Some(sym::usize),
2297+
ty::UintTy::U8 => Some(sym::u8),
2298+
ty::UintTy::U16 => Some(sym::u16),
2299+
ty::UintTy::U32 => Some(sym::u32),
2300+
ty::UintTy::U64 => Some(sym::u64),
2301+
ty::UintTy::U128 => Some(sym::u128),
2302+
},
2303+
_ => None,
2304+
}
2305+
}
22772306
}
22782307

22792308
/// Extra information about why we ended up with a particular variance.
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
pub mod blah{
2-
pub mod baz{
1+
pub mod blah {
2+
pub mod baz {
33
pub struct Foo;
44
}
55
}
66

7-
pub mod meh{
7+
pub mod meh {
88
pub struct Foo;
99
}
1010

@@ -15,4 +15,4 @@ fn foo() -> Foo {
1515
//~^ ERROR mismatched types [E0308]
1616
}
1717

18-
fn main(){}
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
enum Option<T>{
1+
enum Option<T> {
22
Some(T),
33
None,
44
}
55

6-
pub fn foo() -> Option<u8>{
6+
pub fn foo() -> Option<u8> {
77
Some(42_u8)
88
//~^ ERROR mismatched types [E0308]
99
}
1010

11-
fn main(){}
11+
fn main() {}

src/test/ui/mismatched_types/similar_paths.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error[E0308]: mismatched types
22
--> $DIR/similar_paths.rs:7:5
33
|
4-
LL | pub fn foo() -> Option<u8>{
4+
LL | pub fn foo() -> Option<u8> {
55
| ---------- expected `Option<u8>` because of return type
66
LL | Some(42_u8)
77
| ^^^^^^^^^^^ expected enum `Option`, found enum `std::option::Option`
@@ -15,7 +15,7 @@ LL | pub enum Option<T> {
1515
note: enum `Option` is defined in the current crate
1616
--> $DIR/similar_paths.rs:1:1
1717
|
18-
LL | enum Option<T>{
18+
LL | enum Option<T> {
1919
| ^^^^^^^^^^^^^^
2020

2121
error: aborting due to previous error

0 commit comments

Comments
 (0)