From ad1e6298ed785ad0d327231725db501be00f14c5 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:03:14 +0100 Subject: [PATCH 1/3] Make duplicate lang items fatal Prevents terminal spam. --- compiler/rustc_passes/src/lang_items.rs | 4 +++- tests/ui/traits/issue-102989.rs | 1 - tests/ui/traits/issue-102989.stderr | 20 +++----------------- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs index 6aeaa8945fb59..9a21397789d6c 100644 --- a/compiler/rustc_passes/src/lang_items.rs +++ b/compiler/rustc_passes/src/lang_items.rs @@ -149,7 +149,9 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> { } }; - self.tcx.dcx().emit_err(DuplicateLangItem { + // When there's a duplicate lang item, something went very wrong and there's no value in recovering or doing anything. + // Give the user the one message to let them debug the mess they created and then wish them farewell. + self.tcx.dcx().emit_fatal(DuplicateLangItem { local_span: item_span, lang_item_name, crate_name, diff --git a/tests/ui/traits/issue-102989.rs b/tests/ui/traits/issue-102989.rs index f1ecee0a552a7..216cd78e56f9a 100644 --- a/tests/ui/traits/issue-102989.rs +++ b/tests/ui/traits/issue-102989.rs @@ -7,7 +7,6 @@ trait Sized { } //~ ERROR found duplicate lang item `sized` fn ref_Struct(self: &Struct, f: &u32) -> &u32 { //~^ ERROR `self` parameter is only allowed in associated functions //~| ERROR cannot find type `Struct` in this scope - //~| ERROR mismatched types let x = x << 1; //~^ ERROR cannot find value `x` in this scope } diff --git a/tests/ui/traits/issue-102989.stderr b/tests/ui/traits/issue-102989.stderr index 40e49df2b2d96..e17df556d8d21 100644 --- a/tests/ui/traits/issue-102989.stderr +++ b/tests/ui/traits/issue-102989.stderr @@ -13,7 +13,7 @@ LL | fn ref_Struct(self: &Struct, f: &u32) -> &u32 { | ^^^^^^ not found in this scope error[E0425]: cannot find value `x` in this scope - --> $DIR/issue-102989.rs:11:13 + --> $DIR/issue-102989.rs:10:13 | LL | let x = x << 1; | ^ help: a local variable with a similar name exists: `f` @@ -28,21 +28,7 @@ LL | trait Sized { } = note: first definition in `core` loaded from SYSROOT/libcore-*.rlib = note: second definition in the local crate (`issue_102989`) -error[E0308]: mismatched types - --> $DIR/issue-102989.rs:7:42 - | -LL | fn ref_Struct(self: &Struct, f: &u32) -> &u32 { - | ---------- ^^^^ expected `&u32`, found `()` - | | - | implicitly returns `()` as its body has no tail or `return` expression - | -help: consider returning the local binding `f` - | -LL ~ let x = x << 1; -LL + f - | - -error: aborting due to 5 previous errors +error: aborting due to 4 previous errors -Some errors have detailed explanations: E0152, E0308, E0412, E0425. +Some errors have detailed explanations: E0152, E0412, E0425. For more information about an error, try `rustc --explain E0152`. From a360ecd64be275a4ecf2693dbfa058c9b48aa5d6 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:06:26 +0100 Subject: [PATCH 2/3] Delete now-useless test The test was using an internal feature which doesn't really matter, but more importantly, we're now fatally exiting after the duplicate lang item, so this tests nothing. --- tests/ui/traits/issue-102989.rs | 14 ------------ tests/ui/traits/issue-102989.stderr | 34 ----------------------------- 2 files changed, 48 deletions(-) delete mode 100644 tests/ui/traits/issue-102989.rs delete mode 100644 tests/ui/traits/issue-102989.stderr diff --git a/tests/ui/traits/issue-102989.rs b/tests/ui/traits/issue-102989.rs deleted file mode 100644 index 216cd78e56f9a..0000000000000 --- a/tests/ui/traits/issue-102989.rs +++ /dev/null @@ -1,14 +0,0 @@ -// normalize-stderr-test "loaded from .*libcore-.*.rlib" -> "loaded from SYSROOT/libcore-*.rlib" - -#![feature(lang_items)] -#[lang="sized"] -trait Sized { } //~ ERROR found duplicate lang item `sized` - -fn ref_Struct(self: &Struct, f: &u32) -> &u32 { - //~^ ERROR `self` parameter is only allowed in associated functions - //~| ERROR cannot find type `Struct` in this scope - let x = x << 1; - //~^ ERROR cannot find value `x` in this scope -} - -fn main() {} diff --git a/tests/ui/traits/issue-102989.stderr b/tests/ui/traits/issue-102989.stderr deleted file mode 100644 index e17df556d8d21..0000000000000 --- a/tests/ui/traits/issue-102989.stderr +++ /dev/null @@ -1,34 +0,0 @@ -error: `self` parameter is only allowed in associated functions - --> $DIR/issue-102989.rs:7:15 - | -LL | fn ref_Struct(self: &Struct, f: &u32) -> &u32 { - | ^^^^ not semantically valid as function parameter - | - = note: associated functions are those in `impl` or `trait` definitions - -error[E0412]: cannot find type `Struct` in this scope - --> $DIR/issue-102989.rs:7:22 - | -LL | fn ref_Struct(self: &Struct, f: &u32) -> &u32 { - | ^^^^^^ not found in this scope - -error[E0425]: cannot find value `x` in this scope - --> $DIR/issue-102989.rs:10:13 - | -LL | let x = x << 1; - | ^ help: a local variable with a similar name exists: `f` - -error[E0152]: found duplicate lang item `sized` - --> $DIR/issue-102989.rs:5:1 - | -LL | trait Sized { } - | ^^^^^^^^^^^^^^^ - | - = note: the lang item is first defined in crate `core` (which `std` depends on) - = note: first definition in `core` loaded from SYSROOT/libcore-*.rlib - = note: second definition in the local crate (`issue_102989`) - -error: aborting due to 4 previous errors - -Some errors have detailed explanations: E0152, E0412, E0425. -For more information about an error, try `rustc --explain E0152`. From 1f89e900738b30c15894e57d7bdb1c2dbc089976 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:10:01 +0100 Subject: [PATCH 3/3] Add test for duplicate lang items --- tests/ui/lang-items/duplicate.rs | 10 ++++++++++ tests/ui/lang-items/duplicate.stderr | 13 +++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/ui/lang-items/duplicate.rs create mode 100644 tests/ui/lang-items/duplicate.stderr diff --git a/tests/ui/lang-items/duplicate.rs b/tests/ui/lang-items/duplicate.rs new file mode 100644 index 0000000000000..f88d23544145a --- /dev/null +++ b/tests/ui/lang-items/duplicate.rs @@ -0,0 +1,10 @@ +// normalize-stderr-test "loaded from .*libcore-.*.rlib" -> "loaded from SYSROOT/libcore-*.rlib" +#![feature(lang_items)] + +#[lang = "sized"] +trait Sized {} +//~^ ERROR: duplicate lang item + +#[lang = "tuple_trait"] +pub trait Tuple {} +// no error diff --git a/tests/ui/lang-items/duplicate.stderr b/tests/ui/lang-items/duplicate.stderr new file mode 100644 index 0000000000000..aaa8f5e605afa --- /dev/null +++ b/tests/ui/lang-items/duplicate.stderr @@ -0,0 +1,13 @@ +error[E0152]: found duplicate lang item `sized` + --> $DIR/duplicate.rs:5:1 + | +LL | trait Sized {} + | ^^^^^^^^^^^^^^ + | + = note: the lang item is first defined in crate `core` (which `std` depends on) + = note: first definition in `core` loaded from SYSROOT/libcore-*.rlib + = note: second definition in the local crate (`duplicate`) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0152`.