Rollup merge of #129519 - compiler-errors:lowering-flags, r=fmease
Remove redundant flags from `lower_ty_common` that can be inferred from the HIR ...and then get rid of `lower_ty_common`. r? ``@fmease`` or re-roll if you're busy!
This commit is contained in:
commit
ae21236530
@ -392,7 +392,7 @@ fn item_def_id(&self) -> LocalDefId {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
|
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
|
||||||
if let RegionInferReason::BorrowedObjectLifetimeDefault = reason {
|
if let RegionInferReason::ObjectLifetimeDefault = reason {
|
||||||
let e = struct_span_code_err!(
|
let e = struct_span_code_err!(
|
||||||
self.dcx(),
|
self.dcx(),
|
||||||
span,
|
span,
|
||||||
|
@ -15,11 +15,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||||||
///
|
///
|
||||||
/// *Bare* trait object types are ones that aren't preceded by the keyword `dyn`.
|
/// *Bare* trait object types are ones that aren't preceded by the keyword `dyn`.
|
||||||
/// In edition 2021 and onward we emit a hard error for them.
|
/// In edition 2021 and onward we emit a hard error for them.
|
||||||
pub(super) fn prohibit_or_lint_bare_trait_object_ty(
|
pub(super) fn prohibit_or_lint_bare_trait_object_ty(&self, self_ty: &hir::Ty<'_>) {
|
||||||
&self,
|
|
||||||
self_ty: &hir::Ty<'_>,
|
|
||||||
in_path: bool,
|
|
||||||
) {
|
|
||||||
let tcx = self.tcx();
|
let tcx = self.tcx();
|
||||||
|
|
||||||
let hir::TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
|
let hir::TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
|
||||||
@ -28,6 +24,21 @@ pub(super) fn prohibit_or_lint_bare_trait_object_ty(
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let in_path = match tcx.parent_hir_node(self_ty.hir_id) {
|
||||||
|
hir::Node::Ty(hir::Ty {
|
||||||
|
kind: hir::TyKind::Path(hir::QPath::TypeRelative(qself, _)),
|
||||||
|
..
|
||||||
|
})
|
||||||
|
| hir::Node::Expr(hir::Expr {
|
||||||
|
kind: hir::ExprKind::Path(hir::QPath::TypeRelative(qself, _)),
|
||||||
|
..
|
||||||
|
})
|
||||||
|
| hir::Node::Pat(hir::Pat {
|
||||||
|
kind: hir::PatKind::Path(hir::QPath::TypeRelative(qself, _)),
|
||||||
|
..
|
||||||
|
}) if qself.hir_id == self_ty.hir_id => true,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
let needs_bracket = in_path
|
let needs_bracket = in_path
|
||||||
&& !tcx
|
&& !tcx
|
||||||
.sess
|
.sess
|
||||||
|
@ -85,10 +85,9 @@ pub enum PredicateFilter {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum RegionInferReason<'a> {
|
pub enum RegionInferReason<'a> {
|
||||||
/// Lifetime on a trait object behind a reference.
|
/// Lifetime on a trait object that is spelled explicitly, e.g. `+ 'a` or `+ '_`.
|
||||||
/// This allows inferring information from the reference.
|
ExplicitObjectLifetime,
|
||||||
BorrowedObjectLifetimeDefault,
|
/// A trait object's lifetime when it is elided, e.g. `dyn Any`.
|
||||||
/// A trait object's lifetime.
|
|
||||||
ObjectLifetimeDefault,
|
ObjectLifetimeDefault,
|
||||||
/// Generic lifetime parameter
|
/// Generic lifetime parameter
|
||||||
Param(&'a ty::GenericParamDef),
|
Param(&'a ty::GenericParamDef),
|
||||||
@ -1999,16 +1998,6 @@ pub(crate) fn lower_const_param(&self, hir_id: HirId) -> Const<'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lower a type from the HIR to our internal notion of a type.
|
|
||||||
pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
|
|
||||||
self.lower_ty_common(hir_ty, false, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Lower a type inside of a path from the HIR to our internal notion of a type.
|
|
||||||
pub fn lower_ty_in_path(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
|
|
||||||
self.lower_ty_common(hir_ty, false, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
|
fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
|
||||||
let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
|
let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
|
||||||
match idx {
|
match idx {
|
||||||
@ -2026,7 +2015,7 @@ fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
|
|||||||
/// 2. `in_path`: Whether the type appears inside of a path.
|
/// 2. `in_path`: Whether the type appears inside of a path.
|
||||||
/// Used to provide correct diagnostics for bare trait object types.
|
/// Used to provide correct diagnostics for bare trait object types.
|
||||||
#[instrument(level = "debug", skip(self), ret)]
|
#[instrument(level = "debug", skip(self), ret)]
|
||||||
fn lower_ty_common(&self, hir_ty: &hir::Ty<'tcx>, borrowed: bool, in_path: bool) -> Ty<'tcx> {
|
pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
|
||||||
let tcx = self.tcx();
|
let tcx = self.tcx();
|
||||||
|
|
||||||
let result_ty = match &hir_ty.kind {
|
let result_ty = match &hir_ty.kind {
|
||||||
@ -2036,7 +2025,7 @@ fn lower_ty_common(&self, hir_ty: &hir::Ty<'tcx>, borrowed: bool, in_path: bool)
|
|||||||
hir::TyKind::Ref(region, mt) => {
|
hir::TyKind::Ref(region, mt) => {
|
||||||
let r = self.lower_lifetime(region, RegionInferReason::Reference);
|
let r = self.lower_lifetime(region, RegionInferReason::Reference);
|
||||||
debug!(?r);
|
debug!(?r);
|
||||||
let t = self.lower_ty_common(mt.ty, true, false);
|
let t = self.lower_ty(mt.ty);
|
||||||
Ty::new_ref(tcx, r, t, mt.mutbl)
|
Ty::new_ref(tcx, r, t, mt.mutbl)
|
||||||
}
|
}
|
||||||
hir::TyKind::Never => tcx.types.never,
|
hir::TyKind::Never => tcx.types.never,
|
||||||
@ -2065,20 +2054,13 @@ fn lower_ty_common(&self, hir_ty: &hir::Ty<'tcx>, borrowed: bool, in_path: bool)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
hir::TyKind::TraitObject(bounds, lifetime, repr) => {
|
hir::TyKind::TraitObject(bounds, lifetime, repr) => {
|
||||||
self.prohibit_or_lint_bare_trait_object_ty(hir_ty, in_path);
|
self.prohibit_or_lint_bare_trait_object_ty(hir_ty);
|
||||||
|
|
||||||
let repr = match repr {
|
let repr = match repr {
|
||||||
TraitObjectSyntax::Dyn | TraitObjectSyntax::None => ty::Dyn,
|
TraitObjectSyntax::Dyn | TraitObjectSyntax::None => ty::Dyn,
|
||||||
TraitObjectSyntax::DynStar => ty::DynStar,
|
TraitObjectSyntax::DynStar => ty::DynStar,
|
||||||
};
|
};
|
||||||
self.lower_trait_object_ty(
|
self.lower_trait_object_ty(hir_ty.span, hir_ty.hir_id, bounds, lifetime, repr)
|
||||||
hir_ty.span,
|
|
||||||
hir_ty.hir_id,
|
|
||||||
bounds,
|
|
||||||
lifetime,
|
|
||||||
borrowed,
|
|
||||||
repr,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
|
hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
|
||||||
debug!(?maybe_qself, ?path);
|
debug!(?maybe_qself, ?path);
|
||||||
@ -2106,7 +2088,7 @@ fn lower_ty_common(&self, hir_ty: &hir::Ty<'tcx>, borrowed: bool, in_path: bool)
|
|||||||
}
|
}
|
||||||
hir::TyKind::Path(hir::QPath::TypeRelative(qself, segment)) => {
|
hir::TyKind::Path(hir::QPath::TypeRelative(qself, segment)) => {
|
||||||
debug!(?qself, ?segment);
|
debug!(?qself, ?segment);
|
||||||
let ty = self.lower_ty_common(qself, false, true);
|
let ty = self.lower_ty(qself);
|
||||||
self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
|
self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
|
||||||
.map(|(ty, _, _)| ty)
|
.map(|(ty, _, _)| ty)
|
||||||
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))
|
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))
|
||||||
|
@ -30,7 +30,6 @@ pub(super) fn lower_trait_object_ty(
|
|||||||
hir_id: hir::HirId,
|
hir_id: hir::HirId,
|
||||||
hir_trait_bounds: &[(hir::PolyTraitRef<'tcx>, hir::TraitBoundModifier)],
|
hir_trait_bounds: &[(hir::PolyTraitRef<'tcx>, hir::TraitBoundModifier)],
|
||||||
lifetime: &hir::Lifetime,
|
lifetime: &hir::Lifetime,
|
||||||
borrowed: bool,
|
|
||||||
representation: DynKind,
|
representation: DynKind,
|
||||||
) -> Ty<'tcx> {
|
) -> Ty<'tcx> {
|
||||||
let tcx = self.tcx();
|
let tcx = self.tcx();
|
||||||
@ -325,22 +324,32 @@ pub(super) fn lower_trait_object_ty(
|
|||||||
v.dedup();
|
v.dedup();
|
||||||
let existential_predicates = tcx.mk_poly_existential_predicates(&v);
|
let existential_predicates = tcx.mk_poly_existential_predicates(&v);
|
||||||
|
|
||||||
// Use explicitly-specified region bound.
|
// Use explicitly-specified region bound, unless the bound is missing.
|
||||||
let region_bound = if !lifetime.is_elided() {
|
let region_bound = if !lifetime.is_elided() {
|
||||||
self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault)
|
self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime)
|
||||||
} else {
|
} else {
|
||||||
self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
|
self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
|
||||||
|
// Curiously, we prefer object lifetime default for `+ '_`...
|
||||||
if tcx.named_bound_var(lifetime.hir_id).is_some() {
|
if tcx.named_bound_var(lifetime.hir_id).is_some() {
|
||||||
self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault)
|
self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime)
|
||||||
} else {
|
} else {
|
||||||
self.re_infer(
|
let reason =
|
||||||
span,
|
if let hir::LifetimeName::ImplicitObjectLifetimeDefault = lifetime.res {
|
||||||
if borrowed {
|
if let hir::Node::Ty(hir::Ty {
|
||||||
RegionInferReason::ObjectLifetimeDefault
|
kind: hir::TyKind::Ref(parent_lifetime, _),
|
||||||
|
..
|
||||||
|
}) = tcx.parent_hir_node(hir_id)
|
||||||
|
&& tcx.named_bound_var(parent_lifetime.hir_id).is_none()
|
||||||
|
{
|
||||||
|
// Parent lifetime must have failed to resolve. Don't emit a redundant error.
|
||||||
|
RegionInferReason::ExplicitObjectLifetime
|
||||||
|
} else {
|
||||||
|
RegionInferReason::ObjectLifetimeDefault
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
RegionInferReason::BorrowedObjectLifetimeDefault
|
RegionInferReason::ExplicitObjectLifetime
|
||||||
},
|
};
|
||||||
)
|
self.re_infer(span, reason)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
@ -798,7 +798,7 @@ pub fn resolve_ty_and_res_fully_qualified_call(
|
|||||||
// to be object-safe.
|
// to be object-safe.
|
||||||
// We manually call `register_wf_obligation` in the success path
|
// We manually call `register_wf_obligation` in the success path
|
||||||
// below.
|
// below.
|
||||||
let ty = self.lowerer().lower_ty_in_path(qself);
|
let ty = self.lowerer().lower_ty(qself);
|
||||||
(LoweredTy::from_raw(self, span, ty), qself, segment)
|
(LoweredTy::from_raw(self, span, ty), qself, segment)
|
||||||
}
|
}
|
||||||
QPath::LangItem(..) => {
|
QPath::LangItem(..) => {
|
||||||
|
14
tests/ui/dyn-keyword/suggest-dyn-on-bare-trait-in-pat.rs
Normal file
14
tests/ui/dyn-keyword/suggest-dyn-on-bare-trait-in-pat.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
//@ edition: 2021
|
||||||
|
|
||||||
|
trait Trait {}
|
||||||
|
|
||||||
|
impl dyn Trait {
|
||||||
|
const CONST: () = ();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
match () {
|
||||||
|
Trait::CONST => {}
|
||||||
|
//~^ ERROR trait objects must include the `dyn` keyword
|
||||||
|
}
|
||||||
|
}
|
14
tests/ui/dyn-keyword/suggest-dyn-on-bare-trait-in-pat.stderr
Normal file
14
tests/ui/dyn-keyword/suggest-dyn-on-bare-trait-in-pat.stderr
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
error[E0782]: trait objects must include the `dyn` keyword
|
||||||
|
--> $DIR/suggest-dyn-on-bare-trait-in-pat.rs:11:9
|
||||||
|
|
|
||||||
|
LL | Trait::CONST => {}
|
||||||
|
| ^^^^^
|
||||||
|
|
|
||||||
|
help: add `dyn` keyword before this trait
|
||||||
|
|
|
||||||
|
LL | <dyn Trait>::CONST => {}
|
||||||
|
| ++++ +
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0782`.
|
Loading…
Reference in New Issue
Block a user