Skip to content

Commit f352354

Browse files
committed
Address more review comments
- Add back various diagnostic methods on `Session`. It seems unfortunate to duplicate these in so many places, but in the meantime, making the API inconsistent between `Session` and `Diagnostic` also seems unfortunate. - Add back TyCtxtAt methods These will hopefully be used in the near future. - Add back `with_const`, it would need to be added soon after anyway. - Add back `split()` and `get_mut()`, they're useful.
1 parent 230e396 commit f352354

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

compiler/rustc_middle/src/ty/context.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::middle::stability;
1414
use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
1515
use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted};
1616
use crate::traits;
17-
use crate::ty::query::{self, OnDiskCache};
17+
use crate::ty::query::{self, OnDiskCache, TyCtxtAt};
1818
use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts};
1919
use crate::ty::TyKind::*;
2020
use crate::ty::{
@@ -2650,6 +2650,21 @@ impl<'tcx> TyCtxt<'tcx> {
26502650
}
26512651
}
26522652

2653+
impl TyCtxtAt<'tcx> {
2654+
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.
2655+
#[track_caller]
2656+
pub fn ty_error(self) -> Ty<'tcx> {
2657+
self.tcx.ty_error_with_message(self.span, "TyKind::Error constructed but no error reported")
2658+
}
2659+
2660+
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` with the given `msg to
2661+
/// ensure it gets used.
2662+
#[track_caller]
2663+
pub fn ty_error_with_message(self, msg: &str) -> Ty<'tcx> {
2664+
self.tcx.ty_error_with_message(self.span, msg)
2665+
}
2666+
}
2667+
26532668
pub trait InternAs<T: ?Sized, R> {
26542669
type Output;
26552670
fn intern_with<F>(self, f: F) -> Self::Output

compiler/rustc_middle/src/ty/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,11 @@ pub trait WithConstness: Sized {
12081208
ConstnessAnd { constness, value: self }
12091209
}
12101210

1211+
#[inline]
1212+
fn with_const(self) -> ConstnessAnd<Self> {
1213+
self.with_constness(Constness::Const)
1214+
}
1215+
12111216
#[inline]
12121217
fn without_const(self) -> ConstnessAnd<Self> {
12131218
self.with_constness(Constness::NotConst)

compiler/rustc_middle/src/ty/sty.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,20 @@ impl<T> Binder<T> {
10581058
{
10591059
Binder(f(self.0, u.0))
10601060
}
1061+
1062+
/// Splits the contents into two things that share the same binder
1063+
/// level as the original, returning two distinct binders.
1064+
///
1065+
/// `f` should consider bound regions at depth 1 to be free, and
1066+
/// anything it produces with bound regions at depth 1 will be
1067+
/// bound in the resulting return values.
1068+
pub fn split<U, V, F>(self, f: F) -> (Binder<U>, Binder<V>)
1069+
where
1070+
F: FnOnce(T) -> (U, V),
1071+
{
1072+
let (u, v) = f(self.0);
1073+
(Binder(u), Binder(v))
1074+
}
10611075
}
10621076

10631077
impl<T> Binder<Option<T>> {

compiler/rustc_mir/src/interpret/machine.rs

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ pub trait AllocMap<K: Hash + Eq, V> {
7171
fn get(&self, k: K) -> Option<&V> {
7272
self.get_or(k, || Err(())).ok()
7373
}
74+
75+
/// Mutable lookup.
76+
fn get_mut(&mut self, k: K) -> Option<&mut V> {
77+
self.get_mut_or(k, || Err(())).ok()
78+
}
7479
}
7580

7681
/// Methods of this trait signifies a point where CTFE evaluation would fail

compiler/rustc_session/src/session.rs

+41
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ impl Session {
364364
pub fn struct_span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
365365
self.diagnostic().struct_span_warn(sp, msg)
366366
}
367+
pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
368+
&self,
369+
sp: S,
370+
msg: &str,
371+
code: DiagnosticId,
372+
) -> DiagnosticBuilder<'_> {
373+
self.diagnostic().struct_span_warn_with_code(sp, msg, code)
374+
}
367375
pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
368376
self.diagnostic().struct_warn(msg)
369377
}
@@ -402,16 +410,37 @@ impl Session {
402410
) -> DiagnosticBuilder<'_> {
403411
self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
404412
}
413+
pub fn struct_fatal(&self, msg: &str) -> DiagnosticBuilder<'_> {
414+
self.diagnostic().struct_fatal(msg)
415+
}
405416

406417
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
407418
self.diagnostic().span_fatal(sp, msg).raise()
408419
}
420+
pub fn span_fatal_with_code<S: Into<MultiSpan>>(
421+
&self,
422+
sp: S,
423+
msg: &str,
424+
code: DiagnosticId,
425+
) -> ! {
426+
self.diagnostic().span_fatal_with_code(sp, msg, code).raise()
427+
}
409428
pub fn fatal(&self, msg: &str) -> ! {
410429
self.diagnostic().fatal(msg).raise()
411430
}
431+
pub fn span_err_or_warn<S: Into<MultiSpan>>(&self, is_warning: bool, sp: S, msg: &str) {
432+
if is_warning {
433+
self.span_warn(sp, msg);
434+
} else {
435+
self.span_err(sp, msg);
436+
}
437+
}
412438
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
413439
self.diagnostic().span_err(sp, msg)
414440
}
441+
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
442+
self.diagnostic().span_err_with_code(sp, &msg, code)
443+
}
415444
pub fn err(&self, msg: &str) {
416445
self.diagnostic().err(msg)
417446
}
@@ -451,9 +480,18 @@ impl Session {
451480
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
452481
self.diagnostic().span_warn(sp, msg)
453482
}
483+
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
484+
self.diagnostic().span_warn_with_code(sp, msg, code)
485+
}
454486
pub fn warn(&self, msg: &str) {
455487
self.diagnostic().warn(msg)
456488
}
489+
pub fn opt_span_warn<S: Into<MultiSpan>>(&self, opt_sp: Option<S>, msg: &str) {
490+
match opt_sp {
491+
Some(sp) => self.span_warn(sp, msg),
492+
None => self.warn(msg),
493+
}
494+
}
457495
/// Delay a span_bug() call until abort_if_errors()
458496
#[track_caller]
459497
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
@@ -480,6 +518,9 @@ impl Session {
480518
pub fn note_without_error(&self, msg: &str) {
481519
self.diagnostic().note_without_error(msg)
482520
}
521+
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
522+
self.diagnostic().span_note_without_error(sp, msg)
523+
}
483524
pub fn struct_note_without_error(&self, msg: &str) -> DiagnosticBuilder<'_> {
484525
self.diagnostic().struct_note_without_error(msg)
485526
}

0 commit comments

Comments
 (0)