diff options
| author | Joshua Nelson <jyn514@gmail.com> | 2021-03-27 22:16:17 -0400 |
|---|---|---|
| committer | Joshua Nelson <jyn514@gmail.com> | 2021-03-27 22:19:32 -0400 |
| commit | f3523544f114a4a4cc2ced3f1a53f5f3bc158751 (patch) | |
| tree | 9d1883421ed2f6c80ab2c9d137a206e4893c946c | |
| parent | 230e396a7663d933d24c3c30f556abbc1dd9405e (diff) | |
| download | rust-f3523544f114a4a4cc2ced3f1a53f5f3bc158751.tar.gz rust-f3523544f114a4a4cc2ced3f1a53f5f3bc158751.zip | |
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.
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 17 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/sty.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_mir/src/interpret/machine.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_session/src/session.rs | 41 |
5 files changed, 81 insertions, 1 deletions
diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index da3bdc5c721..94c1758b25c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -14,7 +14,7 @@ use crate::middle::stability; use crate::mir::interpret::{self, Allocation, ConstValue, Scalar}; use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted}; use crate::traits; -use crate::ty::query::{self, OnDiskCache}; +use crate::ty::query::{self, OnDiskCache, TyCtxtAt}; use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts}; use crate::ty::TyKind::*; use crate::ty::{ @@ -2650,6 +2650,21 @@ impl<'tcx> TyCtxt<'tcx> { } } +impl TyCtxtAt<'tcx> { + /// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used. + #[track_caller] + pub fn ty_error(self) -> Ty<'tcx> { + self.tcx.ty_error_with_message(self.span, "TyKind::Error constructed but no error reported") + } + + /// Constructs a `TyKind::Error` type and registers a `delay_span_bug` with the given `msg to + /// ensure it gets used. + #[track_caller] + pub fn ty_error_with_message(self, msg: &str) -> Ty<'tcx> { + self.tcx.ty_error_with_message(self.span, msg) + } +} + pub trait InternAs<T: ?Sized, R> { type Output; fn intern_with<F>(self, f: F) -> Self::Output diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 6a1ea6df3f0..c0025cf3fc8 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1209,6 +1209,11 @@ pub trait WithConstness: Sized { } #[inline] + fn with_const(self) -> ConstnessAnd<Self> { + self.with_constness(Constness::Const) + } + + #[inline] fn without_const(self) -> ConstnessAnd<Self> { self.with_constness(Constness::NotConst) } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index b42375aec6e..b8fe7aa7fd0 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1058,6 +1058,20 @@ impl<T> Binder<T> { { Binder(f(self.0, u.0)) } + + /// Splits the contents into two things that share the same binder + /// level as the original, returning two distinct binders. + /// + /// `f` should consider bound regions at depth 1 to be free, and + /// anything it produces with bound regions at depth 1 will be + /// bound in the resulting return values. + pub fn split<U, V, F>(self, f: F) -> (Binder<U>, Binder<V>) + where + F: FnOnce(T) -> (U, V), + { + let (u, v) = f(self.0); + (Binder(u), Binder(v)) + } } impl<T> Binder<Option<T>> { diff --git a/compiler/rustc_mir/src/interpret/machine.rs b/compiler/rustc_mir/src/interpret/machine.rs index 593a90cb54d..65869f95639 100644 --- a/compiler/rustc_mir/src/interpret/machine.rs +++ b/compiler/rustc_mir/src/interpret/machine.rs @@ -71,6 +71,11 @@ pub trait AllocMap<K: Hash + Eq, V> { fn get(&self, k: K) -> Option<&V> { self.get_or(k, || Err(())).ok() } + + /// Mutable lookup. + fn get_mut(&mut self, k: K) -> Option<&mut V> { + self.get_mut_or(k, || Err(())).ok() + } } /// Methods of this trait signifies a point where CTFE evaluation would fail diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 621907f19aa..693f427d7af 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -364,6 +364,14 @@ impl Session { pub fn struct_span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> { self.diagnostic().struct_span_warn(sp, msg) } + pub fn struct_span_warn_with_code<S: Into<MultiSpan>>( + &self, + sp: S, + msg: &str, + code: DiagnosticId, + ) -> DiagnosticBuilder<'_> { + self.diagnostic().struct_span_warn_with_code(sp, msg, code) + } pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> { self.diagnostic().struct_warn(msg) } @@ -402,16 +410,37 @@ impl Session { ) -> DiagnosticBuilder<'_> { self.diagnostic().struct_span_fatal_with_code(sp, msg, code) } + pub fn struct_fatal(&self, msg: &str) -> DiagnosticBuilder<'_> { + self.diagnostic().struct_fatal(msg) + } pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! { self.diagnostic().span_fatal(sp, msg).raise() } + pub fn span_fatal_with_code<S: Into<MultiSpan>>( + &self, + sp: S, + msg: &str, + code: DiagnosticId, + ) -> ! { + self.diagnostic().span_fatal_with_code(sp, msg, code).raise() + } pub fn fatal(&self, msg: &str) -> ! { self.diagnostic().fatal(msg).raise() } + pub fn span_err_or_warn<S: Into<MultiSpan>>(&self, is_warning: bool, sp: S, msg: &str) { + if is_warning { + self.span_warn(sp, msg); + } else { + self.span_err(sp, msg); + } + } pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) { self.diagnostic().span_err(sp, msg) } + pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) { + self.diagnostic().span_err_with_code(sp, &msg, code) + } pub fn err(&self, msg: &str) { self.diagnostic().err(msg) } @@ -451,9 +480,18 @@ impl Session { pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) { self.diagnostic().span_warn(sp, msg) } + pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) { + self.diagnostic().span_warn_with_code(sp, msg, code) + } pub fn warn(&self, msg: &str) { self.diagnostic().warn(msg) } + pub fn opt_span_warn<S: Into<MultiSpan>>(&self, opt_sp: Option<S>, msg: &str) { + match opt_sp { + Some(sp) => self.span_warn(sp, msg), + None => self.warn(msg), + } + } /// Delay a span_bug() call until abort_if_errors() #[track_caller] pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) { @@ -480,6 +518,9 @@ impl Session { pub fn note_without_error(&self, msg: &str) { self.diagnostic().note_without_error(msg) } + pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) { + self.diagnostic().span_note_without_error(sp, msg) + } pub fn struct_note_without_error(&self, msg: &str) -> DiagnosticBuilder<'_> { self.diagnostic().struct_note_without_error(msg) } |
