allow non-monomorphize modules to access hard-coded error message through new struct, use fluent message in monomorphize
This commit is contained in:
parent
e9142473df
commit
30c7506655
8 changed files with 29 additions and 13 deletions
|
@ -21,3 +21,6 @@ monomorphize_large_assignments =
|
||||||
moving {$size} bytes
|
moving {$size} bytes
|
||||||
.label = value moved from here
|
.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 = "..."]`
|
.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
|
||||||
|
|
9
compiler/rustc_hir/src/errors.rs
Normal file
9
compiler/rustc_hir/src/errors.rs
Normal 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())
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@
|
||||||
//! * Functions called by the compiler itself.
|
//! * Functions called by the compiler itself.
|
||||||
|
|
||||||
use crate::def_id::DefId;
|
use crate::def_id::DefId;
|
||||||
|
use crate::errors::LangItemError;
|
||||||
use crate::{MethodKind, Target};
|
use crate::{MethodKind, Target};
|
||||||
|
|
||||||
use rustc_ast as ast;
|
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`.
|
/// 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()>"]`,
|
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
|
||||||
/// returns an error message as a string.
|
/// returns an error encapsulating the `LangItem`.
|
||||||
pub fn require(&self, it: LangItem) -> Result<DefId, String> {
|
pub fn require(&self, it: LangItem) -> Result<DefId, LangItemError> {
|
||||||
self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
|
self.items[it as usize].ok_or_else(|| LangItemError(it))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`DefId`]s of all lang items in a group.
|
/// Returns the [`DefId`]s of all lang items in a group.
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub mod def;
|
||||||
pub mod def_path_hash_map;
|
pub mod def_path_hash_map;
|
||||||
pub mod definitions;
|
pub mod definitions;
|
||||||
pub mod diagnostic_items;
|
pub mod diagnostic_items;
|
||||||
|
pub mod errors;
|
||||||
pub use rustc_span::def_id;
|
pub use rustc_span::def_id;
|
||||||
mod hir;
|
mod hir;
|
||||||
pub mod hir_id;
|
pub mod hir_id;
|
||||||
|
|
|
@ -18,11 +18,11 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||||
/// Returns the `DefId` for a given `LangItem`.
|
/// Returns the `DefId` for a given `LangItem`.
|
||||||
/// If not found, fatally aborts compilation.
|
/// If not found, fatally aborts compilation.
|
||||||
pub fn require_lang_item(self, lang_item: LangItem, span: Option<Span>) -> DefId {
|
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 {
|
if let Some(span) = span {
|
||||||
self.sess.span_fatal(span, &msg)
|
self.sess.span_fatal(span, err.to_string())
|
||||||
} else {
|
} else {
|
||||||
self.sess.fatal(&msg)
|
self.sess.fatal(err.to_string())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -207,7 +207,7 @@ use std::iter;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::errors::{FatalError, LargeAssignmentsLint, RecursionLimit, TypeLengthLimit};
|
use crate::errors::{LargeAssignmentsLint, RecursionLimit, RequiresLangItem, TypeLengthLimit};
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub enum MonoItemCollectionMode {
|
pub enum MonoItemCollectionMode {
|
||||||
|
@ -1328,8 +1328,10 @@ impl<'v> RootCollector<'_, 'v> {
|
||||||
|
|
||||||
let start_def_id = match self.tcx.lang_items().require(LangItem::Start) {
|
let start_def_id = match self.tcx.lang_items().require(LangItem::Start) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(error_message) => {
|
Err(lang_item_err) => {
|
||||||
self.tcx.sess.emit_fatal(FatalError { error_message: error_message.clone() });
|
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();
|
let main_ret_ty = self.tcx.fn_sig(main_def_id).output();
|
||||||
|
|
|
@ -33,9 +33,9 @@ pub struct TypeLengthLimit {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
#[diag(monomorphize::fatal_error)]
|
#[diag(monomorphize::requires_lang_item)]
|
||||||
pub struct FatalError {
|
pub struct RequiresLangItem {
|
||||||
pub error_message: String,
|
pub lang_item: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UnusedGenericParams {
|
pub struct UnusedGenericParams {
|
||||||
|
|
|
@ -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 coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));
|
||||||
|
|
||||||
let unsize_trait = tcx.lang_items().require(LangItem::Unsize).unwrap_or_else(|err| {
|
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);
|
let source = tcx.type_of(impl_did);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue