Skip to content

Commit fdce670

Browse files
committed
[WIP] UPDATE - migrate intrinsic.rs to new diagnostic infrastructure
WIP - replacing span_invalid_monomorphization_error function. Still in progress due to its use in codegen_llvm inside macros
1 parent 5f4fd5f commit fdce670

File tree

4 files changed

+74
-60
lines changed

4 files changed

+74
-60
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::value::Value;
1010
use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh};
1111
use rustc_codegen_ssa::common::span_invalid_monomorphization_error;
1212
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
13+
use rustc_codegen_ssa::errors::InvalidMonomorphization;
1314
use rustc_codegen_ssa::mir::operand::OperandRef;
1415
use rustc_codegen_ssa::mir::place::PlaceRef;
1516
use rustc_codegen_ssa::traits::*;
@@ -284,15 +285,11 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
284285
_ => bug!(),
285286
},
286287
None => {
287-
span_invalid_monomorphization_error(
288-
tcx.sess,
288+
tcx.sess.emit_err(InvalidMonomorphization::BasicIntegerType {
289289
span,
290-
&format!(
291-
"invalid monomorphization of `{}` intrinsic: \
292-
expected basic integer type, found `{}`",
293-
name, ty
294-
),
295-
);
290+
name,
291+
ty,
292+
});
296293
return;
297294
}
298295
}

compiler/rustc_codegen_ssa/src/errors.rs

+43
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_errors::{
66
IntoDiagnosticArg,
77
};
88
use rustc_macros::Diagnostic;
9+
use rustc_middle::ty::Ty;
910
use rustc_span::{Span, Symbol};
1011
use std::borrow::Cow;
1112
use std::io::Error;
@@ -568,3 +569,45 @@ pub struct ShuffleIndicesEvaluation {
568569
#[primary_span]
569570
pub span: Span,
570571
}
572+
573+
#[derive(Diagnostic)]
574+
#[diag(codegen_ssa_missing_memory_ordering)]
575+
pub struct MissingMemoryOrdering;
576+
577+
#[derive(Diagnostic)]
578+
#[diag(codegen_ssa_unknown_atomic_ordering)]
579+
pub struct UnknownAtomicOrdering;
580+
581+
#[derive(Diagnostic)]
582+
#[diag(codegen_ssa_atomic_compare_exchange)]
583+
pub struct AtomicCompareExchange;
584+
585+
#[derive(Diagnostic)]
586+
#[diag(codegen_ssa_unknown_atomic_operation)]
587+
pub struct UnknownAtomicOperation;
588+
589+
#[derive(Diagnostic)]
590+
pub enum InvalidMonomorphization<'tcx> {
591+
#[diag(codegen_ssa_invalid_monomorphization_basic_integer_type, code = "E0511")]
592+
BasicIntegerType {
593+
#[primary_span]
594+
span: Span,
595+
name: Symbol,
596+
ty: Ty<'tcx>,
597+
},
598+
599+
#[diag(codegen_ssa_invalid_monomorphization_basic_float_type, code = "E0511")]
600+
BasicFloatType {
601+
#[primary_span]
602+
span: Span,
603+
name: Symbol,
604+
ty: Ty<'tcx>,
605+
},
606+
607+
#[diag(codegen_ssa_invalid_monomorphization_float_to_int_unchecked, code = "E0511")]
608+
FloatToIntUnchecked {
609+
#[primary_span]
610+
span: Span,
611+
ty: Ty<'tcx>,
612+
},
613+
}

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+12-52
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use super::operand::{OperandRef, OperandValue};
22
use super::place::PlaceRef;
33
use super::FunctionCx;
4-
use crate::common::{span_invalid_monomorphization_error, IntPredicate};
4+
use crate::common::IntPredicate;
5+
use crate::errors;
6+
use crate::errors::InvalidMonomorphization;
57
use crate::glue;
68
use crate::meth;
79
use crate::traits::*;
@@ -299,15 +301,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
299301
_ => bug!(),
300302
},
301303
None => {
302-
span_invalid_monomorphization_error(
303-
bx.tcx().sess,
304-
span,
305-
&format!(
306-
"invalid monomorphization of `{}` intrinsic: \
307-
expected basic integer type, found `{}`",
308-
name, ty
309-
),
310-
);
304+
bx.tcx().sess.emit_err(InvalidMonomorphization::BasicIntegerType { span, name, ty });
311305
return;
312306
}
313307
}
@@ -323,45 +317,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
323317
_ => bug!(),
324318
},
325319
None => {
326-
span_invalid_monomorphization_error(
327-
bx.tcx().sess,
328-
span,
329-
&format!(
330-
"invalid monomorphization of `{}` intrinsic: \
331-
expected basic float type, found `{}`",
332-
name, arg_tys[0]
333-
),
334-
);
320+
bx.tcx().sess.emit_err(InvalidMonomorphization::BasicFloatType { span, name, ty: arg_tys[0] });
335321
return;
336322
}
337323
}
338324
}
339325

340326
sym::float_to_int_unchecked => {
341327
if float_type_width(arg_tys[0]).is_none() {
342-
span_invalid_monomorphization_error(
343-
bx.tcx().sess,
344-
span,
345-
&format!(
346-
"invalid monomorphization of `float_to_int_unchecked` \
347-
intrinsic: expected basic float type, \
348-
found `{}`",
349-
arg_tys[0]
350-
),
351-
);
328+
bx.tcx().sess.emit_err(InvalidMonomorphization::FloatToIntUnchecked { span, ty: arg_tys[0] });
352329
return;
353330
}
354331
let Some((_width, signed)) = int_type_width_signed(ret_ty, bx.tcx()) else {
355-
span_invalid_monomorphization_error(
356-
bx.tcx().sess,
357-
span,
358-
&format!(
359-
"invalid monomorphization of `float_to_int_unchecked` \
360-
intrinsic: expected basic integer type, \
361-
found `{}`",
362-
ret_ty
363-
),
364-
);
332+
bx.tcx().sess.emit_err(InvalidMonomorphization::FloatToIntUnchecked { span, ty: ret_ty });
365333
return;
366334
};
367335
if signed {
@@ -396,7 +364,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
396364
use crate::common::{AtomicRmwBinOp, SynchronizationScope};
397365

398366
let Some((instruction, ordering)) = atomic.split_once('_') else {
399-
bx.sess().fatal("Atomic intrinsic missing memory ordering");
367+
bx.sess().emit_fatal(errors::MissingMemoryOrdering);
400368
};
401369

402370
let parse_ordering = |bx: &Bx, s| match s {
@@ -406,25 +374,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
406374
"release" => Release,
407375
"acqrel" => AcquireRelease,
408376
"seqcst" => SequentiallyConsistent,
409-
_ => bx.sess().fatal("unknown ordering in atomic intrinsic"),
377+
_ => bx.sess().emit_fatal(errors::UnknownAtomicOrdering),
410378
};
411379

412380
let invalid_monomorphization = |ty| {
413-
span_invalid_monomorphization_error(
414-
bx.tcx().sess,
415-
span,
416-
&format!(
417-
"invalid monomorphization of `{}` intrinsic: \
418-
expected basic integer type, found `{}`",
419-
name, ty
420-
),
421-
);
381+
bx.tcx().sess.emit_err(InvalidMonomorphization::BasicIntegerType { span, name, ty });
422382
};
423383

424384
match instruction {
425385
"cxchg" | "cxchgweak" => {
426386
let Some((success, failure)) = ordering.split_once('_') else {
427-
bx.sess().fatal("Atomic compare-exchange intrinsic missing failure memory ordering");
387+
bx.sess().emit_fatal(errors::AtomicCompareExchange);
428388
};
429389
let ty = substs.type_at(0);
430390
if int_type_width_signed(ty, bx.tcx()).is_some() || ty.is_unsafe_ptr() {
@@ -523,7 +483,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
523483
"min" => AtomicRmwBinOp::AtomicMin,
524484
"umax" => AtomicRmwBinOp::AtomicUMax,
525485
"umin" => AtomicRmwBinOp::AtomicUMin,
526-
_ => bx.sess().fatal("unknown atomic operation"),
486+
_ => bx.sess().emit_fatal(errors::UnknownAtomicOperation),
527487
};
528488

529489
let ty = substs.type_at(0);

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

+14
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,17 @@ codegen_ssa_invalid_windows_subsystem = invalid windows subsystem `{$subsystem}`
197197
codegen_ssa_erroneous_constant = erroneous constant encountered
198198
199199
codegen_ssa_shuffle_indices_evaluation = could not evaluate shuffle_indices at compile time
200+
201+
codegen_ssa_missing_memory_ordering = Atomic intrinsic missing memory ordering
202+
203+
codegen_ssa_unknown_atomic_ordering = unknown ordering in atomic intrinsic
204+
205+
codegen_ssa_atomic_compare_exchange = Atomic compare-exchange intrinsic missing failure memory ordering
206+
207+
codegen_ssa_unknown_atomic_operation = unknown atomic operation
208+
209+
codegen_ssa_invalid_monomorphization_basic_integer_type = invalid monomorphization of `{$name}` intrinsic: expected basic integer type, found `{$ty}`
210+
211+
codegen_ssa_invalid_monomorphization_basic_float_type = invalid monomorphization of `{$name}` intrinsic: expected basic float type, found `{$ty}`
212+
213+
codegen_ssa_invalid_monomorphization_float_to_int_unchecked = invalid monomorphization of `float_to_int_unchecked` intrinsic: expected basic float type, found `{$ty}`

0 commit comments

Comments
 (0)