From 8477b9b707db74aa97a8390ff562f0e20b5d4d86 Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Sun, 11 Sep 2022 00:46:53 +0200 Subject: [PATCH 1/4] Note if mismatched types have a similar name --- .../src/infer/error_reporting/mod.rs | 52 +++++++++++++++++-- .../fully-qualified-type-name2.stderr | 12 +++++ src/test/ui/issues/issue-56943.stderr | 12 +++++ src/test/ui/mismatched_types/similar_paths.rs | 11 ++++ .../ui/mismatched_types/similar_paths.stderr | 23 ++++++++ .../type/type-mismatch-same-crate-name.stderr | 11 ++++ 6 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/mismatched_types/similar_paths.rs create mode 100644 src/test/ui/mismatched_types/similar_paths.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 67526c2228937..ab76b48847818 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -51,6 +51,7 @@ use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePa use crate::infer; use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type; +use crate::infer::ExpectedFound; use crate::traits::error_reporting::report_object_safety_error; use crate::traits::{ IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode, @@ -1653,8 +1654,51 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { ), Mismatch::Fixed(s) => (s.into(), s.into(), None), }; - match (&terr, expected == found) { - (TypeError::Sorts(values), extra) => { + let looks_similar = |e: ExpectedFound>| { + // We're only interested in adts + if let (Some(e), Some(f)) = (e.expected.ty_adt_def(), e.found.ty_adt_def()) { + // Only compare the last parts of the path. + // `whatever::Foo` is pretty similar to `blah::Foo` + let e_path = self.tcx.def_path(e.did()).data; + let f_path = self.tcx.def_path(f.did()).data; + if let (Some(e), Some(f)) = (e_path.last(), f_path.last()) { + return e.data == f.data; + } + } + false + }; + + match terr { + // If two types mismatch but have similar names, mention that specifically. + TypeError::Sorts(values) if looks_similar(values) => { + let found_adt = values.found.ty_adt_def().unwrap(); + let expected_adt = values.expected.ty_adt_def().unwrap(); + + let found_name = values.found.sort_string(self.tcx); + let expected_name = values.expected.sort_string(self.tcx); + + diag.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types")); + + for (adt, name) in [(found_adt, found_name), (expected_adt, expected_name)] { + let defid = adt.did(); + let def_span = self.tcx.def_span(defid); + + let msg = if defid.is_local() { + format!("{name} is defined in the current crate.") + } else if self.tcx.all_diagnostic_items(()).id_to_name.get(&defid).is_some() + { + // if it's a diagnostic item, it's definitely defined in std/core/alloc + // otherwise might be, might not be. + format!("{name} is defined in the standard library.") + } else { + let crate_name = self.tcx.crate_name(defid.krate); + format!("{name} is defined in crate `{crate_name}`.") + }; + diag.span_note(def_span, msg); + } + } + TypeError::Sorts(values) => { + let extra = expected == found; let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) { (true, ty::Opaque(def_id, _)) => { let sm = self.tcx.sess.source_map(); @@ -1707,10 +1751,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { ); } } - (TypeError::ObjectUnsafeCoercion(_), _) => { + TypeError::ObjectUnsafeCoercion(_) => { diag.note_unsuccessful_coercion(found, expected); } - (_, _) => { + _ => { debug!( "note_type_err: exp_found={:?}, expected={:?} found={:?}", exp_found, expected, found diff --git a/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr b/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr index aed7f72c660df..8729ea1740ce2 100644 --- a/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr +++ b/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr @@ -5,6 +5,18 @@ LL | fn bar(x: x::Foo) -> y::Foo { | ------ expected `y::Foo` because of return type LL | return x; | ^ expected enum `y::Foo`, found enum `x::Foo` + | + = note: enum `x::Foo` and enum `y::Foo` have similar names, but are actually distinct types +note: enum `x::Foo` is defined in the current crate. + --> $DIR/fully-qualified-type-name2.rs:4:5 + | +LL | pub enum Foo { } + | ^^^^^^^^^^^^ +note: enum `y::Foo` is defined in the current crate. + --> $DIR/fully-qualified-type-name2.rs:8:5 + | +LL | pub enum Foo { } + | ^^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/issues/issue-56943.stderr b/src/test/ui/issues/issue-56943.stderr index 74ed5ec0fb6f1..3efa5f6d04093 100644 --- a/src/test/ui/issues/issue-56943.stderr +++ b/src/test/ui/issues/issue-56943.stderr @@ -5,6 +5,18 @@ LL | let _: issue_56943::S = issue_56943::S2; | -------------- ^^^^^^^^^^^^^^^ expected struct `S`, found struct `S2` | | | expected due to this + | + = note: struct `S2` and struct `S` have similar names, but are actually distinct types +note: struct `S2` is defined in crate `issue_56943`. + --> $DIR/auxiliary/issue-56943.rs:2:9 + | +LL | mod m { pub struct S; } + | ^^^^^^^^^^^^ +note: struct `S` is defined in crate `issue_56943`. + --> $DIR/auxiliary/issue-56943.rs:1:1 + | +LL | pub struct S; + | ^^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/mismatched_types/similar_paths.rs b/src/test/ui/mismatched_types/similar_paths.rs new file mode 100644 index 0000000000000..4d3a2a1fcc61c --- /dev/null +++ b/src/test/ui/mismatched_types/similar_paths.rs @@ -0,0 +1,11 @@ +enum Option{ + Some(T), + None, +} + +pub fn foo() -> Option{ + Some(42_u8) + //~^ ERROR mismatched types [E0308] +} + +fn main(){} diff --git a/src/test/ui/mismatched_types/similar_paths.stderr b/src/test/ui/mismatched_types/similar_paths.stderr new file mode 100644 index 0000000000000..c12afd20b9cf5 --- /dev/null +++ b/src/test/ui/mismatched_types/similar_paths.stderr @@ -0,0 +1,23 @@ +error[E0308]: mismatched types + --> $DIR/similar_paths.rs:7:5 + | +LL | pub fn foo() -> Option{ + | ---------- expected `Option` because of return type +LL | Some(42_u8) + | ^^^^^^^^^^^ expected enum `Option`, found enum `std::option::Option` + | + = note: enum `std::option::Option` and enum `Option` have similar names, but are actually distinct types +note: enum `std::option::Option` is defined in the standard library. + --> $SRC_DIR/core/src/option.rs:LL:COL + | +LL | pub enum Option { + | ^^^^^^^^^^^^^^^^^^ +note: enum `Option` is defined in the current crate. + --> $DIR/similar_paths.rs:1:1 + | +LL | enum Option{ + | ^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/type/type-mismatch-same-crate-name.stderr b/src/test/ui/type/type-mismatch-same-crate-name.stderr index 783f747fa6db6..38a36e8940fa1 100644 --- a/src/test/ui/type/type-mismatch-same-crate-name.stderr +++ b/src/test/ui/type/type-mismatch-same-crate-name.stderr @@ -6,6 +6,17 @@ LL | a::try_foo(foo2); | | | arguments to this function are incorrect | + = note: struct `main::a::Foo` and struct `main::a::Foo` have similar names, but are actually distinct types +note: struct `main::a::Foo` is defined in crate `crate_a2`. + --> $DIR/auxiliary/crate_a2.rs:1:1 + | +LL | pub struct Foo; + | ^^^^^^^^^^^^^^ +note: struct `main::a::Foo` is defined in crate `crate_a1`. + --> $DIR/auxiliary/crate_a1.rs:1:1 + | +LL | pub struct Foo; + | ^^^^^^^^^^^^^^ = note: perhaps two different versions of crate `crate_a1` are being used? note: function defined here --> $DIR/auxiliary/crate_a1.rs:10:8 From 3699c2497bc6ff74126ae8c56a06905f49d0300c Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Mon, 12 Sep 2022 22:17:06 +0200 Subject: [PATCH 2/4] Address feedback --- .../src/infer/error_reporting/mod.rs | 171 ++++++++++++++---- compiler/rustc_infer/src/lib.rs | 2 + .../fully-qualified-type-name2.stderr | 4 +- src/test/ui/issues/issue-56943.stderr | 12 -- src/test/ui/mismatched_types/show_module.rs | 18 ++ .../ui/mismatched_types/show_module.stderr | 23 +++ .../ui/mismatched_types/similar_paths.stderr | 4 +- .../similar_paths_primitive.rs | 10 + .../similar_paths_primitive.stderr | 24 +++ .../type/type-mismatch-same-crate-name.stderr | 4 +- 10 files changed, 223 insertions(+), 49 deletions(-) create mode 100644 src/test/ui/mismatched_types/show_module.rs create mode 100644 src/test/ui/mismatched_types/show_module.stderr create mode 100644 src/test/ui/mismatched_types/similar_paths_primitive.rs create mode 100644 src/test/ui/mismatched_types/similar_paths_primitive.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index ab76b48847818..d4e8e267babe9 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1654,47 +1654,156 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { ), Mismatch::Fixed(s) => (s.into(), s.into(), None), }; - let looks_similar = |e: ExpectedFound>| { - // We're only interested in adts - if let (Some(e), Some(f)) = (e.expected.ty_adt_def(), e.found.ty_adt_def()) { - // Only compare the last parts of the path. - // `whatever::Foo` is pretty similar to `blah::Foo` - let e_path = self.tcx.def_path(e.did()).data; - let f_path = self.tcx.def_path(f.did()).data; - if let (Some(e), Some(f)) = (e_path.last(), f_path.last()) { - return e.data == f.data; + + enum Similar<'tcx> { + Adts(ty::AdtDef<'tcx>, ty::AdtDef<'tcx>), + PrimitiveFound(Ty<'tcx>, ty::AdtDef<'tcx>), + PrimitiveExpected(ty::AdtDef<'tcx>, Ty<'tcx>), + } + + let primitive_sym = |kind: &_| match kind { + ty::Bool => Some(sym::bool), + ty::Char => Some(sym::char), + ty::Float(f) => match f { + ty::FloatTy::F32 => Some(sym::f32), + ty::FloatTy::F64 => Some(sym::f64), + }, + ty::Int(f) => match f { + ty::IntTy::Isize => Some(sym::isize), + ty::IntTy::I8 => Some(sym::i8), + ty::IntTy::I16 => Some(sym::i16), + ty::IntTy::I32 => Some(sym::i32), + ty::IntTy::I64 => Some(sym::i64), + ty::IntTy::I128 => Some(sym::i128), + }, + ty::Uint(f) => match f { + ty::UintTy::Usize => Some(sym::usize), + ty::UintTy::U8 => Some(sym::u8), + ty::UintTy::U16 => Some(sym::u16), + ty::UintTy::U32 => Some(sym::u32), + ty::UintTy::U64 => Some(sym::u64), + ty::UintTy::U128 => Some(sym::u128), + }, + _ => None, + }; + + let similarity = |e: ExpectedFound>| { + let (fk, ek) = (e.found.kind(), e.expected.kind()); + match (fk, ek) { + ( + ty::Adt(adt, _), + ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_), + ) => { + let path = self.tcx.def_path(adt.did()).data; + let name = path.last().unwrap().data.get_opt_name(); + let prim_sym = primitive_sym(ek); + + if name == prim_sym { + return Some(Similar::PrimitiveExpected(*adt, e.expected)); + } + None + } + ( + ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_), + ty::Adt(adt, _), + ) => { + let path = self.tcx.def_path(adt.did()).data; + let name = path.last().unwrap().data.get_opt_name(); + let prim_sym = primitive_sym(fk); + + if name == prim_sym { + return Some(Similar::PrimitiveFound(e.expected, *adt)); + } + None + } + (ty::Adt(f, _), ty::Adt(e, _)) => { + if !f.did().is_local() && f.did().krate == e.did().krate { + // Most likely types from different versions of the same crate + // are in play, in which case this message isn't so helpful. + // A "perhaps two different versions..." error is already emitted for that. + return None; + } + let e_path = self.tcx.def_path(e.did()).data; + let f_path = self.tcx.def_path(f.did()).data; + if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last()) && e_last == f_last { + return Some(Similar::Adts(*f, *e)); + } + None } + _ => None, } - false }; match terr { // If two types mismatch but have similar names, mention that specifically. - TypeError::Sorts(values) if looks_similar(values) => { - let found_adt = values.found.ty_adt_def().unwrap(); - let expected_adt = values.expected.ty_adt_def().unwrap(); - - let found_name = values.found.sort_string(self.tcx); - let expected_name = values.expected.sort_string(self.tcx); + TypeError::Sorts(values) if let Some(s) = similarity(values) => { + let diagnose_primitive = + |prim: Ty<'tcx>, + shadow: Ty<'tcx>, + defid: DefId, + diagnostic: &mut Diagnostic| { + let name = shadow.sort_string(self.tcx); + diagnostic.note(format!( + "{prim} and {name} have similar names, but are actually distinct types" + )); + diagnostic + .note(format!("{prim} is a primitive defined by the language")); + let def_span = self.tcx.def_span(defid); + let msg = if defid.is_local() { + format!("{name} is defined in the current crate") + } else { + let crate_name = self.tcx.crate_name(defid.krate); + format!("{name} is defined in crate `{crate_name}") + }; + diagnostic.span_note(def_span, msg); + }; - diag.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types")); + let diagnose_adts = + |found_adt: ty::AdtDef<'tcx>, + expected_adt: ty::AdtDef<'tcx>, + diagnostic: &mut Diagnostic| { + let found_name = values.found.sort_string(self.tcx); + let expected_name = values.expected.sort_string(self.tcx); - for (adt, name) in [(found_adt, found_name), (expected_adt, expected_name)] { - let defid = adt.did(); - let def_span = self.tcx.def_span(defid); + let found_defid = found_adt.did(); + let expected_defid = expected_adt.did(); - let msg = if defid.is_local() { - format!("{name} is defined in the current crate.") - } else if self.tcx.all_diagnostic_items(()).id_to_name.get(&defid).is_some() - { - // if it's a diagnostic item, it's definitely defined in std/core/alloc - // otherwise might be, might not be. - format!("{name} is defined in the standard library.") - } else { - let crate_name = self.tcx.crate_name(defid.krate); - format!("{name} is defined in crate `{crate_name}`.") + diagnostic.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types")); + for (defid, name) in + [(found_defid, found_name), (expected_defid, expected_name)] + { + let def_span = self.tcx.def_span(defid); + + let msg = if found_defid.is_local() && expected_defid.is_local() { + let module = self + .tcx + .parent_module_from_def_id(defid.expect_local()) + .to_def_id(); + let module_name = + self.tcx.def_path(module).to_string_no_crate_verbose(); + format!( + "{name} is defined in module {module_name} of the current crate" + ) + } else if defid.is_local() { + format!("{name} is defined in the current crate") + } else { + let crate_name = self.tcx.crate_name(defid.krate); + format!("{name} is defined in crate `{crate_name}`") + }; + diagnostic.span_note(def_span, msg); + } }; - diag.span_note(def_span, msg); + + match s { + Similar::Adts(found_adt, expected_adt) => { + diagnose_adts(found_adt, expected_adt, diag) + } + Similar::PrimitiveFound(prim, e) => { + diagnose_primitive(prim, values.expected, e.did(), diag) + } + Similar::PrimitiveExpected(f, prim) => { + diagnose_primitive(prim, values.found, f.did(), diag) + } } } TypeError::Sorts(values) => { diff --git a/compiler/rustc_infer/src/lib.rs b/compiler/rustc_infer/src/lib.rs index ef60d2c918845..5b14e5bdb78cf 100644 --- a/compiler/rustc_infer/src/lib.rs +++ b/compiler/rustc_infer/src/lib.rs @@ -20,6 +20,8 @@ #![cfg_attr(bootstrap, feature(label_break_value))] #![feature(let_chains)] #![cfg_attr(bootstrap, feature(let_else))] +#![feature(let_else)] +#![feature(if_let_guard)] #![feature(min_specialization)] #![feature(never_type)] #![feature(try_blocks)] diff --git a/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr b/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr index 8729ea1740ce2..94c34cf9d04fa 100644 --- a/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr +++ b/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr @@ -7,12 +7,12 @@ LL | return x; | ^ expected enum `y::Foo`, found enum `x::Foo` | = note: enum `x::Foo` and enum `y::Foo` have similar names, but are actually distinct types -note: enum `x::Foo` is defined in the current crate. +note: enum `x::Foo` is defined in module ::x of the current crate --> $DIR/fully-qualified-type-name2.rs:4:5 | LL | pub enum Foo { } | ^^^^^^^^^^^^ -note: enum `y::Foo` is defined in the current crate. +note: enum `y::Foo` is defined in module ::y of the current crate --> $DIR/fully-qualified-type-name2.rs:8:5 | LL | pub enum Foo { } diff --git a/src/test/ui/issues/issue-56943.stderr b/src/test/ui/issues/issue-56943.stderr index 3efa5f6d04093..74ed5ec0fb6f1 100644 --- a/src/test/ui/issues/issue-56943.stderr +++ b/src/test/ui/issues/issue-56943.stderr @@ -5,18 +5,6 @@ LL | let _: issue_56943::S = issue_56943::S2; | -------------- ^^^^^^^^^^^^^^^ expected struct `S`, found struct `S2` | | | expected due to this - | - = note: struct `S2` and struct `S` have similar names, but are actually distinct types -note: struct `S2` is defined in crate `issue_56943`. - --> $DIR/auxiliary/issue-56943.rs:2:9 - | -LL | mod m { pub struct S; } - | ^^^^^^^^^^^^ -note: struct `S` is defined in crate `issue_56943`. - --> $DIR/auxiliary/issue-56943.rs:1:1 - | -LL | pub struct S; - | ^^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/mismatched_types/show_module.rs b/src/test/ui/mismatched_types/show_module.rs new file mode 100644 index 0000000000000..97d45b377bcfb --- /dev/null +++ b/src/test/ui/mismatched_types/show_module.rs @@ -0,0 +1,18 @@ +pub mod blah{ + pub mod baz{ + pub struct Foo; + } +} + +pub mod meh{ + pub struct Foo; +} + +pub type Foo = blah::baz::Foo; + +fn foo() -> Foo { + meh::Foo + //~^ ERROR mismatched types [E0308] +} + +fn main(){} diff --git a/src/test/ui/mismatched_types/show_module.stderr b/src/test/ui/mismatched_types/show_module.stderr new file mode 100644 index 0000000000000..6d8986d1cad2c --- /dev/null +++ b/src/test/ui/mismatched_types/show_module.stderr @@ -0,0 +1,23 @@ +error[E0308]: mismatched types + --> $DIR/show_module.rs:14:5 + | +LL | fn foo() -> Foo { + | --- expected `baz::Foo` because of return type +LL | meh::Foo + | ^^^^^^^^ expected struct `baz::Foo`, found struct `meh::Foo` + | + = note: struct `meh::Foo` and struct `baz::Foo` have similar names, but are actually distinct types +note: struct `meh::Foo` is defined in module ::meh of the current crate + --> $DIR/show_module.rs:8:5 + | +LL | pub struct Foo; + | ^^^^^^^^^^^^^^ +note: struct `baz::Foo` is defined in module ::blah::baz of the current crate + --> $DIR/show_module.rs:3:9 + | +LL | pub struct Foo; + | ^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/mismatched_types/similar_paths.stderr b/src/test/ui/mismatched_types/similar_paths.stderr index c12afd20b9cf5..16f33e55fea03 100644 --- a/src/test/ui/mismatched_types/similar_paths.stderr +++ b/src/test/ui/mismatched_types/similar_paths.stderr @@ -7,12 +7,12 @@ LL | Some(42_u8) | ^^^^^^^^^^^ expected enum `Option`, found enum `std::option::Option` | = note: enum `std::option::Option` and enum `Option` have similar names, but are actually distinct types -note: enum `std::option::Option` is defined in the standard library. +note: enum `std::option::Option` is defined in crate `core` --> $SRC_DIR/core/src/option.rs:LL:COL | LL | pub enum Option { | ^^^^^^^^^^^^^^^^^^ -note: enum `Option` is defined in the current crate. +note: enum `Option` is defined in the current crate --> $DIR/similar_paths.rs:1:1 | LL | enum Option{ diff --git a/src/test/ui/mismatched_types/similar_paths_primitive.rs b/src/test/ui/mismatched_types/similar_paths_primitive.rs new file mode 100644 index 0000000000000..8f5b7cce46908 --- /dev/null +++ b/src/test/ui/mismatched_types/similar_paths_primitive.rs @@ -0,0 +1,10 @@ +#![allow(non_camel_case_types)] + +struct bool; + +fn foo(_: bool) {} + +fn main() { + foo(true); + //~^ ERROR mismatched types [E0308] +} diff --git a/src/test/ui/mismatched_types/similar_paths_primitive.stderr b/src/test/ui/mismatched_types/similar_paths_primitive.stderr new file mode 100644 index 0000000000000..8a2f73945e841 --- /dev/null +++ b/src/test/ui/mismatched_types/similar_paths_primitive.stderr @@ -0,0 +1,24 @@ +error[E0308]: mismatched types + --> $DIR/similar_paths_primitive.rs:8:9 + | +LL | foo(true); + | --- ^^^^ expected struct `bool`, found `bool` + | | + | arguments to this function are incorrect + | + = note: bool and struct `bool` have similar names, but are actually distinct types + = note: bool is a primitive defined by the language +note: struct `bool` is defined in the current crate + --> $DIR/similar_paths_primitive.rs:3:1 + | +LL | struct bool; + | ^^^^^^^^^^^ +note: function defined here + --> $DIR/similar_paths_primitive.rs:5:4 + | +LL | fn foo(_: bool) {} + | ^^^ ------- + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/type/type-mismatch-same-crate-name.stderr b/src/test/ui/type/type-mismatch-same-crate-name.stderr index 38a36e8940fa1..fcafd315ebf54 100644 --- a/src/test/ui/type/type-mismatch-same-crate-name.stderr +++ b/src/test/ui/type/type-mismatch-same-crate-name.stderr @@ -7,12 +7,12 @@ LL | a::try_foo(foo2); | arguments to this function are incorrect | = note: struct `main::a::Foo` and struct `main::a::Foo` have similar names, but are actually distinct types -note: struct `main::a::Foo` is defined in crate `crate_a2`. +note: struct `main::a::Foo` is defined in crate `crate_a2` --> $DIR/auxiliary/crate_a2.rs:1:1 | LL | pub struct Foo; | ^^^^^^^^^^^^^^ -note: struct `main::a::Foo` is defined in crate `crate_a1`. +note: struct `main::a::Foo` is defined in crate `crate_a1` --> $DIR/auxiliary/crate_a1.rs:1:1 | LL | pub struct Foo; From 14e98930239e0a419d9662cde6e4a3aac6564f5a Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Sun, 18 Sep 2022 02:59:32 +0200 Subject: [PATCH 3/4] Print out the proper crate path --- compiler/rustc_infer/src/infer/error_reporting/mod.rs | 7 ++----- compiler/rustc_infer/src/lib.rs | 1 - .../fully-qualified-type/fully-qualified-type-name2.stderr | 4 ++-- src/test/ui/mismatched_types/show_module.stderr | 4 ++-- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index d4e8e267babe9..dbe068ff174b3 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1779,11 +1779,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { .tcx .parent_module_from_def_id(defid.expect_local()) .to_def_id(); - let module_name = - self.tcx.def_path(module).to_string_no_crate_verbose(); - format!( - "{name} is defined in module {module_name} of the current crate" - ) + let module_name = self.tcx.def_path(module).to_string_no_crate_verbose(); + format!("{name} is defined in module `crate{module_name}` of the current crate") } else if defid.is_local() { format!("{name} is defined in the current crate") } else { diff --git a/compiler/rustc_infer/src/lib.rs b/compiler/rustc_infer/src/lib.rs index 5b14e5bdb78cf..909a597e22135 100644 --- a/compiler/rustc_infer/src/lib.rs +++ b/compiler/rustc_infer/src/lib.rs @@ -20,7 +20,6 @@ #![cfg_attr(bootstrap, feature(label_break_value))] #![feature(let_chains)] #![cfg_attr(bootstrap, feature(let_else))] -#![feature(let_else)] #![feature(if_let_guard)] #![feature(min_specialization)] #![feature(never_type)] diff --git a/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr b/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr index 94c34cf9d04fa..a8f23f81dea02 100644 --- a/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr +++ b/src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr @@ -7,12 +7,12 @@ LL | return x; | ^ expected enum `y::Foo`, found enum `x::Foo` | = note: enum `x::Foo` and enum `y::Foo` have similar names, but are actually distinct types -note: enum `x::Foo` is defined in module ::x of the current crate +note: enum `x::Foo` is defined in module `crate::x` of the current crate --> $DIR/fully-qualified-type-name2.rs:4:5 | LL | pub enum Foo { } | ^^^^^^^^^^^^ -note: enum `y::Foo` is defined in module ::y of the current crate +note: enum `y::Foo` is defined in module `crate::y` of the current crate --> $DIR/fully-qualified-type-name2.rs:8:5 | LL | pub enum Foo { } diff --git a/src/test/ui/mismatched_types/show_module.stderr b/src/test/ui/mismatched_types/show_module.stderr index 6d8986d1cad2c..5e48e0955aae9 100644 --- a/src/test/ui/mismatched_types/show_module.stderr +++ b/src/test/ui/mismatched_types/show_module.stderr @@ -7,12 +7,12 @@ LL | meh::Foo | ^^^^^^^^ expected struct `baz::Foo`, found struct `meh::Foo` | = note: struct `meh::Foo` and struct `baz::Foo` have similar names, but are actually distinct types -note: struct `meh::Foo` is defined in module ::meh of the current crate +note: struct `meh::Foo` is defined in module `crate::meh` of the current crate --> $DIR/show_module.rs:8:5 | LL | pub struct Foo; | ^^^^^^^^^^^^^^ -note: struct `baz::Foo` is defined in module ::blah::baz of the current crate +note: struct `baz::Foo` is defined in module `crate::blah::baz` of the current crate --> $DIR/show_module.rs:3:9 | LL | pub struct Foo; From c65866000e07270817465f3cbb8b1d01ecdb9a20 Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Mon, 19 Sep 2022 17:19:45 +0200 Subject: [PATCH 4/4] Incorporate feedback --- .../src/infer/error_reporting/mod.rs | 115 ++++++------------ compiler/rustc_middle/src/ty/sty.rs | 31 ++++- src/test/ui/mismatched_types/show_module.rs | 8 +- src/test/ui/mismatched_types/similar_paths.rs | 6 +- .../ui/mismatched_types/similar_paths.stderr | 4 +- 5 files changed, 75 insertions(+), 89 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index dbe068ff174b3..aba2447fa68e9 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1656,82 +1656,39 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { }; enum Similar<'tcx> { - Adts(ty::AdtDef<'tcx>, ty::AdtDef<'tcx>), - PrimitiveFound(Ty<'tcx>, ty::AdtDef<'tcx>), - PrimitiveExpected(ty::AdtDef<'tcx>, Ty<'tcx>), - } - - let primitive_sym = |kind: &_| match kind { - ty::Bool => Some(sym::bool), - ty::Char => Some(sym::char), - ty::Float(f) => match f { - ty::FloatTy::F32 => Some(sym::f32), - ty::FloatTy::F64 => Some(sym::f64), - }, - ty::Int(f) => match f { - ty::IntTy::Isize => Some(sym::isize), - ty::IntTy::I8 => Some(sym::i8), - ty::IntTy::I16 => Some(sym::i16), - ty::IntTy::I32 => Some(sym::i32), - ty::IntTy::I64 => Some(sym::i64), - ty::IntTy::I128 => Some(sym::i128), - }, - ty::Uint(f) => match f { - ty::UintTy::Usize => Some(sym::usize), - ty::UintTy::U8 => Some(sym::u8), - ty::UintTy::U16 => Some(sym::u16), - ty::UintTy::U32 => Some(sym::u32), - ty::UintTy::U64 => Some(sym::u64), - ty::UintTy::U128 => Some(sym::u128), - }, - _ => None, - }; - - let similarity = |e: ExpectedFound>| { - let (fk, ek) = (e.found.kind(), e.expected.kind()); - match (fk, ek) { - ( - ty::Adt(adt, _), - ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_), - ) => { - let path = self.tcx.def_path(adt.did()).data; - let name = path.last().unwrap().data.get_opt_name(); - let prim_sym = primitive_sym(ek); + Adts { expected: ty::AdtDef<'tcx>, found: ty::AdtDef<'tcx> }, + PrimitiveFound { expected: ty::AdtDef<'tcx>, found: Ty<'tcx> }, + PrimitiveExpected { expected: Ty<'tcx>, found: ty::AdtDef<'tcx> }, + } - if name == prim_sym { - return Some(Similar::PrimitiveExpected(*adt, e.expected)); - } - None + let similarity = |ExpectedFound { expected, found }: ExpectedFound>| { + if let ty::Adt(expected, _) = expected.kind() && let Some(primitive) = found.primitive_symbol() { + let path = self.tcx.def_path(expected.did()).data; + let name = path.last().unwrap().data.get_opt_name(); + if name == Some(primitive) { + return Some(Similar::PrimitiveFound { expected: *expected, found }); } - ( - ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_), - ty::Adt(adt, _), - ) => { - let path = self.tcx.def_path(adt.did()).data; - let name = path.last().unwrap().data.get_opt_name(); - let prim_sym = primitive_sym(fk); - - if name == prim_sym { - return Some(Similar::PrimitiveFound(e.expected, *adt)); - } - None + } else if let Some(primitive) = expected.primitive_symbol() && let ty::Adt(found, _) = found.kind() { + let path = self.tcx.def_path(found.did()).data; + let name = path.last().unwrap().data.get_opt_name(); + if name == Some(primitive) { + return Some(Similar::PrimitiveExpected { expected, found: *found }); } - (ty::Adt(f, _), ty::Adt(e, _)) => { - if !f.did().is_local() && f.did().krate == e.did().krate { - // Most likely types from different versions of the same crate - // are in play, in which case this message isn't so helpful. - // A "perhaps two different versions..." error is already emitted for that. - return None; - } - let e_path = self.tcx.def_path(e.did()).data; - let f_path = self.tcx.def_path(f.did()).data; - if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last()) && e_last == f_last { - return Some(Similar::Adts(*f, *e)); - } - None + } else if let ty::Adt(expected, _) = expected.kind() && let ty::Adt(found, _) = found.kind() { + if !expected.did().is_local() && expected.did().krate == found.did().krate { + // Most likely types from different versions of the same crate + // are in play, in which case this message isn't so helpful. + // A "perhaps two different versions..." error is already emitted for that. + return None; + } + let f_path = self.tcx.def_path(found.did()).data; + let e_path = self.tcx.def_path(expected.did()).data; + + if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last()) && e_last == f_last { + return Some(Similar::Adts{expected: *expected, found: *found}); } - _ => None, } + None }; match terr { @@ -1759,8 +1716,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { }; let diagnose_adts = - |found_adt: ty::AdtDef<'tcx>, - expected_adt: ty::AdtDef<'tcx>, + |expected_adt : ty::AdtDef<'tcx>, + found_adt: ty::AdtDef<'tcx>, diagnostic: &mut Diagnostic| { let found_name = values.found.sort_string(self.tcx); let expected_name = values.expected.sort_string(self.tcx); @@ -1792,14 +1749,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { }; match s { - Similar::Adts(found_adt, expected_adt) => { - diagnose_adts(found_adt, expected_adt, diag) + Similar::Adts{expected, found} => { + diagnose_adts(expected, found, diag) } - Similar::PrimitiveFound(prim, e) => { - diagnose_primitive(prim, values.expected, e.did(), diag) + Similar::PrimitiveFound{expected, found: prim} => { + diagnose_primitive(prim, values.expected, expected.did(), diag) } - Similar::PrimitiveExpected(f, prim) => { - diagnose_primitive(prim, values.found, f.did(), diag) + Similar::PrimitiveExpected{expected: prim, found} => { + diagnose_primitive(prim, values.found, found.did(), diag) } } } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 36e5608503904..6583e61931a53 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -19,7 +19,7 @@ use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_index::vec::Idx; use rustc_macros::HashStable; -use rustc_span::symbol::{kw, Symbol}; +use rustc_span::symbol::{kw, sym, Symbol}; use rustc_target::abi::VariantIdx; use rustc_target::spec::abi; use std::borrow::Cow; @@ -2274,6 +2274,35 @@ impl<'tcx> Ty<'tcx> { } } } + + // If `self` is a primitive, return its [`Symbol`]. + pub fn primitive_symbol(self) -> Option { + match self.kind() { + ty::Bool => Some(sym::bool), + ty::Char => Some(sym::char), + ty::Float(f) => match f { + ty::FloatTy::F32 => Some(sym::f32), + ty::FloatTy::F64 => Some(sym::f64), + }, + ty::Int(f) => match f { + ty::IntTy::Isize => Some(sym::isize), + ty::IntTy::I8 => Some(sym::i8), + ty::IntTy::I16 => Some(sym::i16), + ty::IntTy::I32 => Some(sym::i32), + ty::IntTy::I64 => Some(sym::i64), + ty::IntTy::I128 => Some(sym::i128), + }, + ty::Uint(f) => match f { + ty::UintTy::Usize => Some(sym::usize), + ty::UintTy::U8 => Some(sym::u8), + ty::UintTy::U16 => Some(sym::u16), + ty::UintTy::U32 => Some(sym::u32), + ty::UintTy::U64 => Some(sym::u64), + ty::UintTy::U128 => Some(sym::u128), + }, + _ => None, + } + } } /// Extra information about why we ended up with a particular variance. diff --git a/src/test/ui/mismatched_types/show_module.rs b/src/test/ui/mismatched_types/show_module.rs index 97d45b377bcfb..61550b8875520 100644 --- a/src/test/ui/mismatched_types/show_module.rs +++ b/src/test/ui/mismatched_types/show_module.rs @@ -1,10 +1,10 @@ -pub mod blah{ - pub mod baz{ +pub mod blah { + pub mod baz { pub struct Foo; } } -pub mod meh{ +pub mod meh { pub struct Foo; } @@ -15,4 +15,4 @@ fn foo() -> Foo { //~^ ERROR mismatched types [E0308] } -fn main(){} +fn main() {} diff --git a/src/test/ui/mismatched_types/similar_paths.rs b/src/test/ui/mismatched_types/similar_paths.rs index 4d3a2a1fcc61c..4b9157f39bf48 100644 --- a/src/test/ui/mismatched_types/similar_paths.rs +++ b/src/test/ui/mismatched_types/similar_paths.rs @@ -1,11 +1,11 @@ -enum Option{ +enum Option { Some(T), None, } -pub fn foo() -> Option{ +pub fn foo() -> Option { Some(42_u8) //~^ ERROR mismatched types [E0308] } -fn main(){} +fn main() {} diff --git a/src/test/ui/mismatched_types/similar_paths.stderr b/src/test/ui/mismatched_types/similar_paths.stderr index 16f33e55fea03..e65ae58d4ce96 100644 --- a/src/test/ui/mismatched_types/similar_paths.stderr +++ b/src/test/ui/mismatched_types/similar_paths.stderr @@ -1,7 +1,7 @@ error[E0308]: mismatched types --> $DIR/similar_paths.rs:7:5 | -LL | pub fn foo() -> Option{ +LL | pub fn foo() -> Option { | ---------- expected `Option` because of return type LL | Some(42_u8) | ^^^^^^^^^^^ expected enum `Option`, found enum `std::option::Option` @@ -15,7 +15,7 @@ LL | pub enum Option { note: enum `Option` is defined in the current crate --> $DIR/similar_paths.rs:1:1 | -LL | enum Option{ +LL | enum Option { | ^^^^^^^^^^^^^^ error: aborting due to previous error