1
Fork 0

Add ErrorGuaranteed to HIR TyKind::Err

This commit is contained in:
Michael Goulet 2023-02-22 21:19:42 +00:00
parent dcca6a375b
commit a772a6fc2a
9 changed files with 51 additions and 45 deletions

View file

@ -284,7 +284,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))), .alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
}, },
ItemKind::GlobalAsm(asm) => hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm)), ItemKind::GlobalAsm(asm) => hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm)),
ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty: Some(ty), .. }) => { ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty, .. }) => {
// We lower // We lower
// //
// type Foo = impl Trait // type Foo = impl Trait
@ -299,18 +299,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
&generics, &generics,
id, id,
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy), |this| match ty {
None => {
let guar = this.tcx.sess.delay_span_bug(
span,
"expected to lower type alias type, but it was missing",
); );
hir::ItemKind::TyAlias(ty, generics) this.arena.alloc(this.ty(span, hir::TyKind::Err(guar)))
} }
ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty: None, .. }) => { Some(ty) => this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy),
let mut generics = generics.clone(); },
add_ty_alias_where_clause(&mut generics, *where_clauses, true);
let (generics, ty) = self.lower_generics(
&generics,
id,
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| this.arena.alloc(this.ty(span, hir::TyKind::Err)),
); );
hir::ItemKind::TyAlias(ty, generics) hir::ItemKind::TyAlias(ty, generics)
} }
@ -847,7 +845,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| match ty { |this| match ty {
None => { None => {
let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err)); let guar = this.tcx.sess.delay_span_bug(
i.span,
"expected to lower associated type, but it was missing",
);
let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err(guar)));
hir::ImplItemKind::Type(ty) hir::ImplItemKind::Type(ty)
} }
Some(ty) => { Some(ty) => {

View file

@ -1082,11 +1082,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::TypeBindingKind::Constraint { bounds } hir::TypeBindingKind::Constraint { bounds }
} }
DesugarKind::Error(position) => { DesugarKind::Error(position) => {
self.tcx.sess.emit_err(errors::MisplacedAssocTyBinding { let guar = self.tcx.sess.emit_err(errors::MisplacedAssocTyBinding {
span: constraint.span, span: constraint.span,
position: DiagnosticArgFromDisplay(position), position: DiagnosticArgFromDisplay(position),
}); });
let err_ty = &*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err)); let err_ty =
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
hir::TypeBindingKind::Equality { term: err_ty.into() } hir::TypeBindingKind::Equality { term: err_ty.into() }
} }
} }
@ -1255,7 +1256,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_ty_direct(&mut self, t: &Ty, itctx: &ImplTraitContext) -> hir::Ty<'hir> { fn lower_ty_direct(&mut self, t: &Ty, itctx: &ImplTraitContext) -> hir::Ty<'hir> {
let kind = match &t.kind { let kind = match &t.kind {
TyKind::Infer => hir::TyKind::Infer, TyKind::Infer => hir::TyKind::Infer,
TyKind::Err => hir::TyKind::Err, TyKind::Err => {
hir::TyKind::Err(self.tcx.sess.delay_span_bug(t.span, "TyKind::Err lowered"))
}
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)), TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)), TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
TyKind::Ref(region, mt) => { TyKind::Ref(region, mt) => {
@ -1381,7 +1384,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
path path
} }
ImplTraitContext::FeatureGated(position, feature) => { ImplTraitContext::FeatureGated(position, feature) => {
self.tcx let guar = self
.tcx
.sess .sess
.create_feature_err( .create_feature_err(
MisplacedImplTrait { MisplacedImplTrait {
@ -1391,24 +1395,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
*feature, *feature,
) )
.emit(); .emit();
hir::TyKind::Err hir::TyKind::Err(guar)
} }
ImplTraitContext::Disallowed(position) => { ImplTraitContext::Disallowed(position) => {
self.tcx.sess.emit_err(MisplacedImplTrait { let guar = self.tcx.sess.emit_err(MisplacedImplTrait {
span: t.span, span: t.span,
position: DiagnosticArgFromDisplay(position), position: DiagnosticArgFromDisplay(position),
}); });
hir::TyKind::Err hir::TyKind::Err(guar)
} }
} }
} }
TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"), TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
TyKind::CVarArgs => { TyKind::CVarArgs => {
self.tcx.sess.delay_span_bug( let guar = self.tcx.sess.delay_span_bug(
t.span, t.span,
"`TyKind::CVarArgs` should have been handled elsewhere", "`TyKind::CVarArgs` should have been handled elsewhere",
); );
hir::TyKind::Err hir::TyKind::Err(guar)
} }
}; };

View file

@ -42,7 +42,7 @@ pub use rustc_error_messages::{
pub use rustc_lint_defs::{pluralize, Applicability}; pub use rustc_lint_defs::{pluralize, Applicability};
use rustc_macros::fluent_messages; use rustc_macros::fluent_messages;
use rustc_span::source_map::SourceMap; use rustc_span::source_map::SourceMap;
use rustc_span::HashStableContext; pub use rustc_span::ErrorGuaranteed;
use rustc_span::{Loc, Span}; use rustc_span::{Loc, Span};
use std::borrow::Cow; use std::borrow::Cow;
@ -1846,17 +1846,3 @@ pub enum TerminalUrl {
Yes, Yes,
Auto, Auto,
} }
/// Useful type to use with `Result<>` indicate that an error has already
/// been reported to the user, so no need to continue checking.
#[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(HashStable_Generic)]
pub struct ErrorGuaranteed(());
impl ErrorGuaranteed {
/// To be used only if you really know what you are doing... ideally, we would find a way to
/// eliminate all calls to this method.
pub fn unchecked_claim_error_was_emitted() -> Self {
ErrorGuaranteed(())
}
}

View file

@ -369,10 +369,10 @@ impl<'hir> GenericArgs<'hir> {
pub fn has_err(&self) -> bool { pub fn has_err(&self) -> bool {
self.args.iter().any(|arg| match arg { self.args.iter().any(|arg| match arg {
GenericArg::Type(ty) => matches!(ty.kind, TyKind::Err), GenericArg::Type(ty) => matches!(ty.kind, TyKind::Err(_)),
_ => false, _ => false,
}) || self.bindings.iter().any(|arg| match arg.kind { }) || self.bindings.iter().any(|arg| match arg.kind {
TypeBindingKind::Equality { term: Term::Ty(ty) } => matches!(ty.kind, TyKind::Err), TypeBindingKind::Equality { term: Term::Ty(ty) } => matches!(ty.kind, TyKind::Err(_)),
_ => false, _ => false,
}) })
} }
@ -2676,7 +2676,7 @@ pub enum TyKind<'hir> {
/// specified. This can appear anywhere in a type. /// specified. This can appear anywhere in a type.
Infer, Infer,
/// Placeholder for a type that has failed to be defined. /// Placeholder for a type that has failed to be defined.
Err, Err(rustc_span::ErrorGuaranteed),
} }
#[derive(Debug, HashStable_Generic)] #[derive(Debug, HashStable_Generic)]

View file

@ -844,7 +844,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
visitor.visit_lifetime(lifetime); visitor.visit_lifetime(lifetime);
} }
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression), TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
TyKind::Infer | TyKind::Err => {} TyKind::Infer | TyKind::Err(_) => {}
} }
} }

View file

@ -3113,7 +3113,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// handled specially and will not descend into this routine. // handled specially and will not descend into this routine.
self.ty_infer(None, ast_ty.span) self.ty_infer(None, ast_ty.span)
} }
hir::TyKind::Err => tcx.ty_error_misc(), hir::TyKind::Err(guar) => tcx.ty_error(*guar),
}; };
self.record_ty(ast_ty.hir_id, result_ty, ast_ty.span); self.record_ty(ast_ty.hir_id, result_ty, ast_ty.span);

View file

@ -358,7 +358,7 @@ impl<'a> State<'a> {
self.print_anon_const(e); self.print_anon_const(e);
self.word(")"); self.word(")");
} }
hir::TyKind::Err => { hir::TyKind::Err(_) => {
self.popen(); self.popen();
self.word("/*ERROR*/"); self.word("/*ERROR*/");
self.pclose(); self.pclose();

View file

@ -2149,3 +2149,17 @@ where
Hash::hash(&len, hasher); Hash::hash(&len, hasher);
} }
} }
/// Useful type to use with `Result<>` indicate that an error has already
/// been reported to the user, so no need to continue checking.
#[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(HashStable_Generic)]
pub struct ErrorGuaranteed(());
impl ErrorGuaranteed {
/// To be used only if you really know what you are doing... ideally, we would find a way to
/// eliminate all calls to this method.
pub fn unchecked_claim_error_was_emitted() -> Self {
ErrorGuaranteed(())
}
}

View file

@ -1661,7 +1661,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
} }
TyKind::BareFn(barefn) => BareFunction(Box::new(clean_bare_fn_ty(barefn, cx))), TyKind::BareFn(barefn) => BareFunction(Box::new(clean_bare_fn_ty(barefn, cx))),
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s. // Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
TyKind::Infer | TyKind::Err | TyKind::Typeof(..) => Infer, TyKind::Infer | TyKind::Err(_) | TyKind::Typeof(..) => Infer,
} }
} }