Skip to content

Commit 2f74d1d

Browse files
naritanaraCleanCut
authored andcommitted
Migrate weak_lang_items.rs
1 parent bde80f7 commit 2f74d1d

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,12 @@ passes_collapse_debuginfo = `collapse_debuginfo` attribute should be applied to
271271
272272
passes_deprecated_annotation_has_no_effect = this `#[deprecated]` annotation has no effect
273273
.suggestion = remove the unnecessary deprecation attribute
274+
275+
passes_missing_panic_handler = `#[panic_handler]` function required, but not found
276+
277+
passes_missing_alloc_error_handler = `#[alloc_error_handler]` function required, but not found
278+
.note = use `#![feature(default_alloc_error_handler)]` for a default error handler
279+
280+
passes_missing_lang_item = language item required, but not found: `{$name}`
281+
.note = this can occur when a binary crate with `#![no_std]` is compiled for a target where `{$name}` is defined in the standard library
282+
.help = you may be able to compile for a target that doesn't need `{$name}`, specify a target with `--target` or in `.cargo/config`

compiler/rustc_passes/src/errors.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,20 @@ pub struct DeprecatedAnnotationHasNoEffect {
665665
#[suggestion(applicability = "machine-applicable", code = "")]
666666
pub span: Span,
667667
}
668+
669+
#[derive(Diagnostic)]
670+
#[diag(passes::missing_panic_handler)]
671+
pub struct MissingPanicHandler;
672+
673+
#[derive(Diagnostic)]
674+
#[diag(passes::missing_alloc_error_handler)]
675+
#[note]
676+
pub struct MissingAllocErrorHandler;
677+
678+
#[derive(Diagnostic)]
679+
#[diag(passes::missing_lang_item)]
680+
#[note]
681+
#[help]
682+
pub struct MissingLangItem {
683+
pub name: Symbol,
684+
}

compiler/rustc_passes/src/weak_lang_items.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use rustc_middle::middle::lang_items::required;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::config::CrateType;
1010

11+
use crate::errors::{MissingAllocErrorHandler, MissingLangItem, MissingPanicHandler};
12+
1113
/// Checks the crate for usage of weak lang items, returning a vector of all the
1214
/// language items required by this crate, but not defined yet.
1315
pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
@@ -71,20 +73,13 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
7173
for (name, &item) in WEAK_ITEMS_REFS.iter() {
7274
if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() {
7375
if item == LangItem::PanicImpl {
74-
tcx.sess.err("`#[panic_handler]` function required, but not found");
76+
tcx.sess.emit_err(MissingPanicHandler);
7577
} else if item == LangItem::Oom {
7678
if !tcx.features().default_alloc_error_handler {
77-
tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
78-
tcx.sess.note_without_error("use `#![feature(default_alloc_error_handler)]` for a default error handler");
79+
tcx.sess.emit_err(MissingAllocErrorHandler);
7980
}
8081
} else {
81-
tcx
82-
.sess
83-
.diagnostic()
84-
.struct_err(&format!("language item required, but not found: `{}`", name))
85-
.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))
86-
.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))
87-
.emit();
82+
tcx.sess.emit_err(MissingLangItem { name: *name });
8883
}
8984
}
9085
}

0 commit comments

Comments
 (0)