allow non-monomorphize modules to access hard-coded error message through new struct, use fluent message in monomorphize

This commit is contained in:
Nathan Stocks 2022-08-24 16:28:56 -06:00
parent e9142473df
commit 30c7506655
8 changed files with 29 additions and 13 deletions

View file

@ -21,3 +21,6 @@ monomorphize_large_assignments =
moving {$size} bytes
.label = value moved from here
.note = The current maximum size is {$limit}, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]`
monomorphize_requires_lang_item =
requires `{$lang_item}` lang_item

View file

@ -0,0 +1,9 @@
use crate::LangItem;
pub struct LangItemError(pub LangItem);
impl ToString for LangItemError {
fn to_string(&self) -> String {
format!("requires `{}` lang_item", self.0.name())
}
}

View file

@ -8,6 +8,7 @@
//! * Functions called by the compiler itself.
use crate::def_id::DefId;
use crate::errors::LangItemError;
use crate::{MethodKind, Target};
use rustc_ast as ast;
@ -115,9 +116,9 @@ macro_rules! language_item_table {
/// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
/// returns an error message as a string.
pub fn require(&self, it: LangItem) -> Result<DefId, String> {
self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
/// returns an error encapsulating the `LangItem`.
pub fn require(&self, it: LangItem) -> Result<DefId, LangItemError> {
self.items[it as usize].ok_or_else(|| LangItemError(it))
}
/// Returns the [`DefId`]s of all lang items in a group.

View file

@ -27,6 +27,7 @@ pub mod def;
pub mod def_path_hash_map;
pub mod definitions;
pub mod diagnostic_items;
pub mod errors;
pub use rustc_span::def_id;
mod hir;
pub mod hir_id;

View file

@ -18,11 +18,11 @@ impl<'tcx> TyCtxt<'tcx> {
/// Returns the `DefId` for a given `LangItem`.
/// If not found, fatally aborts compilation.
pub fn require_lang_item(self, lang_item: LangItem, span: Option<Span>) -> DefId {
self.lang_items().require(lang_item).unwrap_or_else(|msg| {
self.lang_items().require(lang_item).unwrap_or_else(|err| {
if let Some(span) = span {
self.sess.span_fatal(span, &msg)
self.sess.span_fatal(span, err.to_string())
} else {
self.sess.fatal(&msg)
self.sess.fatal(err.to_string())
}
})
}

View file

@ -207,7 +207,7 @@ use std::iter;
use std::ops::Range;
use std::path::PathBuf;
use crate::errors::{FatalError, LargeAssignmentsLint, RecursionLimit, TypeLengthLimit};
use crate::errors::{LargeAssignmentsLint, RecursionLimit, RequiresLangItem, TypeLengthLimit};
#[derive(PartialEq)]
pub enum MonoItemCollectionMode {
@ -1328,8 +1328,10 @@ impl<'v> RootCollector<'_, 'v> {
let start_def_id = match self.tcx.lang_items().require(LangItem::Start) {
Ok(s) => s,
Err(error_message) => {
self.tcx.sess.emit_fatal(FatalError { error_message: error_message.clone() });
Err(lang_item_err) => {
self.tcx
.sess
.emit_fatal(RequiresLangItem { lang_item: lang_item_err.0.name().to_string() });
}
};
let main_ret_ty = self.tcx.fn_sig(main_def_id).output();

View file

@ -33,9 +33,9 @@ pub struct TypeLengthLimit {
}
#[derive(SessionDiagnostic)]
#[diag(monomorphize::fatal_error)]
pub struct FatalError {
pub error_message: String,
#[diag(monomorphize::requires_lang_item)]
pub struct RequiresLangItem {
pub lang_item: String,
}
pub struct UnusedGenericParams {

View file

@ -359,7 +359,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));
let unsize_trait = tcx.lang_items().require(LangItem::Unsize).unwrap_or_else(|err| {
tcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err));
tcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err.to_string()));
});
let source = tcx.type_of(impl_did);