From fae40c507068b2d7aa5dc7bda729762e536aa8c2 Mon Sep 17 00:00:00 2001
From: Lucas Kent <rubickent@gmail.com>
Date: Sun, 12 Dec 2021 13:04:00 +1100
Subject: [PATCH] Suggest to specify a target triple when eh_personality lang
 item is missing

---
 compiler/rustc_passes/src/weak_lang_items.rs           | 10 ++++++++--
 src/test/ui/extern-flag/empty-extern-arg.stderr        |  3 +++
 src/test/ui/missing/missing-alloc_error_handler.stderr |  2 +-
 src/test/ui/panic-handler/weak-lang-item.stderr        |  3 +++
 4 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/compiler/rustc_passes/src/weak_lang_items.rs b/compiler/rustc_passes/src/weak_lang_items.rs
index c6c32e69aab77..61c82f031dd5b 100644
--- a/compiler/rustc_passes/src/weak_lang_items.rs
+++ b/compiler/rustc_passes/src/weak_lang_items.rs
@@ -67,10 +67,16 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
             } else if item == LangItem::Oom {
                 if !tcx.features().default_alloc_error_handler {
                     tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
-                    tcx.sess.note_without_error("Use `#![feature(default_alloc_error_handler)]` for a default error handler");
+                    tcx.sess.note_without_error("use `#![feature(default_alloc_error_handler)]` for a default error handler");
                 }
             } else {
-                tcx.sess.err(&format!("language item required, but not found: `{}`", name));
+                tcx
+                    .sess
+                    .diagnostic()
+                    .struct_err(&format!("language item required, but not found: `{}`", name))
+                    .note(&format!("this can occur when a binary crate with `#![no_std]` is compiled for a target where `{}` is defined in the standard library", name))
+                    .help(&format!("you may be able to compile for a target that doesn't need `{}`, specify a target with `--target` or in `.cargo/config`", name))
+                    .emit();
             }
         }
     }
diff --git a/src/test/ui/extern-flag/empty-extern-arg.stderr b/src/test/ui/extern-flag/empty-extern-arg.stderr
index b0628a4f6dd62..39a66c08de098 100644
--- a/src/test/ui/extern-flag/empty-extern-arg.stderr
+++ b/src/test/ui/extern-flag/empty-extern-arg.stderr
@@ -1,6 +1,9 @@
 error: extern location for std does not exist: 
 
 error: language item required, but not found: `eh_personality`
+   |
+   = note: this can occur when a binary crate with `#![no_std]` is compiled for a target where `eh_personality` is defined in the standard library
+   = help: you may be able to compile for a target that doesn't need `eh_personality`, specify a target with `--target` or in `.cargo/config`
 
 error: `#[panic_handler]` function required, but not found
 
diff --git a/src/test/ui/missing/missing-alloc_error_handler.stderr b/src/test/ui/missing/missing-alloc_error_handler.stderr
index ed84493deb563..995fa7cf85e96 100644
--- a/src/test/ui/missing/missing-alloc_error_handler.stderr
+++ b/src/test/ui/missing/missing-alloc_error_handler.stderr
@@ -1,6 +1,6 @@
 error: `#[alloc_error_handler]` function required, but not found
 
-note: Use `#![feature(default_alloc_error_handler)]` for a default error handler
+note: use `#![feature(default_alloc_error_handler)]` for a default error handler
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/panic-handler/weak-lang-item.stderr b/src/test/ui/panic-handler/weak-lang-item.stderr
index 1f14b20e4514d..cc25f08e33a80 100644
--- a/src/test/ui/panic-handler/weak-lang-item.stderr
+++ b/src/test/ui/panic-handler/weak-lang-item.stderr
@@ -11,6 +11,9 @@ LL | extern crate core as other_core;
    |
 
 error: language item required, but not found: `eh_personality`
+   |
+   = note: this can occur when a binary crate with `#![no_std]` is compiled for a target where `eh_personality` is defined in the standard library
+   = help: you may be able to compile for a target that doesn't need `eh_personality`, specify a target with `--target` or in `.cargo/config`
 
 error: `#[panic_handler]` function required, but not found