Skip to content

Commit 30c7506

Browse files
committed
allow non-monomorphize modules to access hard-coded error message through new struct, use fluent message in monomorphize
1 parent e914247 commit 30c7506

File tree

8 files changed

+29
-13
lines changed

8 files changed

+29
-13
lines changed

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

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

compiler/rustc_hir/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use crate::LangItem;
2+
3+
pub struct LangItemError(pub LangItem);
4+
5+
impl ToString for LangItemError {
6+
fn to_string(&self) -> String {
7+
format!("requires `{}` lang_item", self.0.name())
8+
}
9+
}

compiler/rustc_hir/src/lang_items.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! * Functions called by the compiler itself.
99
1010
use crate::def_id::DefId;
11+
use crate::errors::LangItemError;
1112
use crate::{MethodKind, Target};
1213

1314
use rustc_ast as ast;
@@ -115,9 +116,9 @@ macro_rules! language_item_table {
115116

116117
/// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
117118
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
118-
/// returns an error message as a string.
119-
pub fn require(&self, it: LangItem) -> Result<DefId, String> {
120-
self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
119+
/// returns an error encapsulating the `LangItem`.
120+
pub fn require(&self, it: LangItem) -> Result<DefId, LangItemError> {
121+
self.items[it as usize].ok_or_else(|| LangItemError(it))
121122
}
122123

123124
/// Returns the [`DefId`]s of all lang items in a group.

compiler/rustc_hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub mod def;
2727
pub mod def_path_hash_map;
2828
pub mod definitions;
2929
pub mod diagnostic_items;
30+
pub mod errors;
3031
pub use rustc_span::def_id;
3132
mod hir;
3233
pub mod hir_id;

compiler/rustc_middle/src/middle/lang_items.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ impl<'tcx> TyCtxt<'tcx> {
1818
/// Returns the `DefId` for a given `LangItem`.
1919
/// If not found, fatally aborts compilation.
2020
pub fn require_lang_item(self, lang_item: LangItem, span: Option<Span>) -> DefId {
21-
self.lang_items().require(lang_item).unwrap_or_else(|msg| {
21+
self.lang_items().require(lang_item).unwrap_or_else(|err| {
2222
if let Some(span) = span {
23-
self.sess.span_fatal(span, &msg)
23+
self.sess.span_fatal(span, err.to_string())
2424
} else {
25-
self.sess.fatal(&msg)
25+
self.sess.fatal(err.to_string())
2626
}
2727
})
2828
}

compiler/rustc_monomorphize/src/collector.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ use std::iter;
207207
use std::ops::Range;
208208
use std::path::PathBuf;
209209

210-
use crate::errors::{FatalError, LargeAssignmentsLint, RecursionLimit, TypeLengthLimit};
210+
use crate::errors::{LargeAssignmentsLint, RecursionLimit, RequiresLangItem, TypeLengthLimit};
211211

212212
#[derive(PartialEq)]
213213
pub enum MonoItemCollectionMode {
@@ -1328,8 +1328,10 @@ impl<'v> RootCollector<'_, 'v> {
13281328

13291329
let start_def_id = match self.tcx.lang_items().require(LangItem::Start) {
13301330
Ok(s) => s,
1331-
Err(error_message) => {
1332-
self.tcx.sess.emit_fatal(FatalError { error_message: error_message.clone() });
1331+
Err(lang_item_err) => {
1332+
self.tcx
1333+
.sess
1334+
.emit_fatal(RequiresLangItem { lang_item: lang_item_err.0.name().to_string() });
13331335
}
13341336
};
13351337
let main_ret_ty = self.tcx.fn_sig(main_def_id).output();

compiler/rustc_monomorphize/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ pub struct TypeLengthLimit {
3333
}
3434

3535
#[derive(SessionDiagnostic)]
36-
#[diag(monomorphize::fatal_error)]
37-
pub struct FatalError {
38-
pub error_message: String,
36+
#[diag(monomorphize::requires_lang_item)]
37+
pub struct RequiresLangItem {
38+
pub lang_item: String,
3939
}
4040

4141
pub struct UnusedGenericParams {

compiler/rustc_typeck/src/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
359359
let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));
360360

361361
let unsize_trait = tcx.lang_items().require(LangItem::Unsize).unwrap_or_else(|err| {
362-
tcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err));
362+
tcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err.to_string()));
363363
});
364364

365365
let source = tcx.type_of(impl_did);

0 commit comments

Comments
 (0)