Skip to content

Commit ecd2d11

Browse files
Remove redundant flags that can be inferred from the HIR
1 parent 147bb17 commit ecd2d11

File tree

6 files changed

+50
-29
lines changed

6 files changed

+50
-29
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1515
///
1616
/// *Bare* trait object types are ones that aren't preceded by the keyword `dyn`.
1717
/// In edition 2021 and onward we emit a hard error for them.
18-
pub(super) fn prohibit_or_lint_bare_trait_object_ty(
19-
&self,
20-
self_ty: &hir::Ty<'_>,
21-
in_path: bool,
22-
) {
18+
pub(super) fn prohibit_or_lint_bare_trait_object_ty(&self, self_ty: &hir::Ty<'_>) {
2319
let tcx = self.tcx();
2420

2521
let hir::TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
@@ -28,6 +24,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2824
return;
2925
};
3026

27+
let in_path = match tcx.parent_hir_node(self_ty.hir_id) {
28+
hir::Node::Ty(hir::Ty {
29+
kind: hir::TyKind::Path(hir::QPath::TypeRelative(qself, _)),
30+
..
31+
})
32+
| hir::Node::Expr(hir::Expr {
33+
kind: hir::ExprKind::Path(hir::QPath::TypeRelative(qself, _)),
34+
..
35+
})
36+
| hir::Node::Pat(hir::Pat {
37+
kind: hir::PatKind::Path(hir::QPath::TypeRelative(qself, _)),
38+
..
39+
}) if qself.hir_id == self_ty.hir_id => true,
40+
_ => false,
41+
};
3142
let needs_bracket = in_path
3243
&& !tcx
3344
.sess

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -1998,16 +1998,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
19981998
}
19991999
}
20002000

2001-
/// Lower a type from the HIR to our internal notion of a type.
2002-
pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
2003-
self.lower_ty_common(hir_ty, false, false)
2004-
}
2005-
2006-
/// Lower a type inside of a path from the HIR to our internal notion of a type.
2007-
pub fn lower_ty_in_path(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
2008-
self.lower_ty_common(hir_ty, false, true)
2009-
}
2010-
20112001
fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
20122002
let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
20132003
match idx {
@@ -2025,7 +2015,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20252015
/// 2. `in_path`: Whether the type appears inside of a path.
20262016
/// Used to provide correct diagnostics for bare trait object types.
20272017
#[instrument(level = "debug", skip(self), ret)]
2028-
fn lower_ty_common(&self, hir_ty: &hir::Ty<'tcx>, borrowed: bool, in_path: bool) -> Ty<'tcx> {
2018+
pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
20292019
let tcx = self.tcx();
20302020

20312021
let result_ty = match &hir_ty.kind {
@@ -2035,7 +2025,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20352025
hir::TyKind::Ref(region, mt) => {
20362026
let r = self.lower_lifetime(region, RegionInferReason::Reference);
20372027
debug!(?r);
2038-
let t = self.lower_ty_common(mt.ty, true, false);
2028+
let t = self.lower_ty(mt.ty);
20392029
Ty::new_ref(tcx, r, t, mt.mutbl)
20402030
}
20412031
hir::TyKind::Never => tcx.types.never,
@@ -2064,20 +2054,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20642054
)
20652055
}
20662056
hir::TyKind::TraitObject(bounds, lifetime, repr) => {
2067-
self.prohibit_or_lint_bare_trait_object_ty(hir_ty, in_path);
2057+
self.prohibit_or_lint_bare_trait_object_ty(hir_ty);
20682058

20692059
let repr = match repr {
20702060
TraitObjectSyntax::Dyn | TraitObjectSyntax::None => ty::Dyn,
20712061
TraitObjectSyntax::DynStar => ty::DynStar,
20722062
};
2073-
self.lower_trait_object_ty(
2074-
hir_ty.span,
2075-
hir_ty.hir_id,
2076-
bounds,
2077-
lifetime,
2078-
borrowed,
2079-
repr,
2080-
)
2063+
self.lower_trait_object_ty(hir_ty.span, hir_ty.hir_id, bounds, lifetime, repr)
20812064
}
20822065
hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
20832066
debug!(?maybe_qself, ?path);
@@ -2105,7 +2088,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21052088
}
21062089
hir::TyKind::Path(hir::QPath::TypeRelative(qself, segment)) => {
21072090
debug!(?qself, ?segment);
2108-
let ty = self.lower_ty_common(qself, false, true);
2091+
let ty = self.lower_ty(qself);
21092092
self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
21102093
.map(|(ty, _, _)| ty)
21112094
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))

compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
3030
hir_id: hir::HirId,
3131
hir_trait_bounds: &[(hir::PolyTraitRef<'tcx>, hir::TraitBoundModifier)],
3232
lifetime: &hir::Lifetime,
33-
_borrowed: bool,
3433
representation: DynKind,
3534
) -> Ty<'tcx> {
3635
let tcx = self.tcx();

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
798798
// to be object-safe.
799799
// We manually call `register_wf_obligation` in the success path
800800
// below.
801-
let ty = self.lowerer().lower_ty_in_path(qself);
801+
let ty = self.lowerer().lower_ty(qself);
802802
(LoweredTy::from_raw(self, span, ty), qself, segment)
803803
}
804804
QPath::LangItem(..) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ edition: 2021
2+
3+
trait Trait {}
4+
5+
impl dyn Trait {
6+
const CONST: () = ();
7+
}
8+
9+
fn main() {
10+
match () {
11+
Trait::CONST => {}
12+
//~^ ERROR trait objects must include the `dyn` keyword
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0782]: trait objects must include the `dyn` keyword
2+
--> $DIR/suggest-dyn-on-bare-trait-in-pat.rs:11:9
3+
|
4+
LL | Trait::CONST => {}
5+
| ^^^^^
6+
|
7+
help: add `dyn` keyword before this trait
8+
|
9+
LL | <dyn Trait>::CONST => {}
10+
| ++++ +
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0782`.

0 commit comments

Comments
 (0)