Skip to content

Commit b83c65e

Browse files
committed
HIR ty lowering: Simplify signature of lower_poly_trait_ref
1 parent 7ba34c7 commit b83c65e

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
482482

483483
match hir_bound {
484484
hir::GenericBound::Trait(poly_trait_ref) => {
485-
let hir::TraitBoundModifiers { constness, polarity } = poly_trait_ref.modifiers;
486485
let _ = self.lower_poly_trait_ref(
487-
&poly_trait_ref.trait_ref,
488-
poly_trait_ref.span,
489-
constness,
490-
polarity,
486+
poly_trait_ref,
491487
param_ty,
492488
bounds,
493489
predicate_filter,

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use tracing::{debug, instrument};
1818

1919
use super::HirTyLowerer;
2020
use crate::errors::SelfInTypeAlias;
21-
use crate::hir_ty_lowering::{
22-
GenericArgCountMismatch, GenericArgCountResult, PredicateFilter, RegionInferReason,
23-
};
21+
use crate::hir_ty_lowering::{GenericArgCountMismatch, PredicateFilter, RegionInferReason};
2422

2523
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2624
/// Lower a trait object type from the HIR to our internal notion of a type.
@@ -38,24 +36,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
3836

3937
let mut user_written_bounds = Vec::new();
4038
let mut potential_assoc_types = Vec::new();
41-
for trait_bound in hir_bounds.iter() {
42-
if let hir::BoundPolarity::Maybe(_) = trait_bound.modifiers.polarity {
39+
for poly_trait_ref in hir_bounds.iter() {
40+
if let hir::BoundPolarity::Maybe(_) = poly_trait_ref.modifiers.polarity {
4341
continue;
4442
}
45-
if let GenericArgCountResult {
46-
correct:
47-
Err(GenericArgCountMismatch { invalid_args: cur_potential_assoc_types, .. }),
48-
..
49-
} = self.lower_poly_trait_ref(
50-
&trait_bound.trait_ref,
51-
trait_bound.span,
52-
hir::BoundConstness::Never,
53-
hir::BoundPolarity::Positive,
43+
let result = self.lower_poly_trait_ref(
44+
poly_trait_ref,
5445
dummy_self,
5546
&mut user_written_bounds,
5647
PredicateFilter::SelfOnly,
57-
) {
58-
potential_assoc_types.extend(cur_potential_assoc_types);
48+
);
49+
if let Err(GenericArgCountMismatch { invalid_args, .. }) = result.correct {
50+
potential_assoc_types.extend(invalid_args);
5951
}
6052
}
6153

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,17 +750,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
750750
/// If for example you had `for<'a> Foo<'a>: Bar<'a>`, then the `self_ty` would be `Foo<'a>`
751751
/// where `'a` is a bound region at depth 0. Similarly, the `trait_ref` would be `Bar<'a>`.
752752
/// The lowered poly-trait-ref will track this binder explicitly, however.
753-
#[instrument(level = "debug", skip(self, span, constness, bounds))]
753+
#[instrument(level = "debug", skip(self, bounds))]
754754
pub(crate) fn lower_poly_trait_ref(
755755
&self,
756-
trait_ref: &hir::TraitRef<'tcx>,
757-
span: Span,
758-
constness: hir::BoundConstness,
759-
polarity: hir::BoundPolarity,
756+
poly_trait_ref: &hir::PolyTraitRef<'tcx>,
760757
self_ty: Ty<'tcx>,
761758
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
762759
predicate_filter: PredicateFilter,
763760
) -> GenericArgCountResult {
761+
// We use the *resolved* bound vars later instead of the HIR ones since the former
762+
// also include the bound vars of the overarching predicate if applicable.
763+
let hir::PolyTraitRef { bound_generic_params: _, modifiers, ref trait_ref, span } =
764+
*poly_trait_ref;
765+
let hir::TraitBoundModifiers { constness, polarity } = modifiers;
766+
764767
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
765768
let trait_segment = trait_ref.path.segments.last().unwrap();
766769

tests/ui/traits/const-traits/const-trait-bounds-trait-objects.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ fn main() {
1414
trait NonConst {}
1515
const fn handle(_: &dyn const NonConst) {}
1616
//~^ ERROR const trait bounds are not allowed in trait object types
17+
//~| ERROR `const` can only be applied to `#[const_trait]` traits
1718
const fn take(_: &dyn [const] NonConst) {}
1819
//~^ ERROR `[const]` is not allowed here
20+
//~| ERROR `[const]` can only be applied to `#[const_trait]` traits

tests/ui/traits/const-traits/const-trait-bounds-trait-objects.stderr

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,34 @@ LL | const fn handle(_: &dyn const NonConst) {}
1919
| ^^^^^^^^^^^^^^
2020

2121
error: `[const]` is not allowed here
22-
--> $DIR/const-trait-bounds-trait-objects.rs:17:19
22+
--> $DIR/const-trait-bounds-trait-objects.rs:18:19
2323
|
2424
LL | const fn take(_: &dyn [const] NonConst) {}
2525
| ^^^^^^^^^^^
2626
|
2727
= note: trait objects cannot have `[const]` trait bounds
2828

29-
error: aborting due to 4 previous errors
29+
error: `const` can only be applied to `#[const_trait]` traits
30+
--> $DIR/const-trait-bounds-trait-objects.rs:15:25
31+
|
32+
LL | const fn handle(_: &dyn const NonConst) {}
33+
| ^^^^^ can't be applied to `NonConst`
34+
|
35+
help: mark `NonConst` as `#[const_trait]` to allow it to have `const` implementations
36+
|
37+
LL | #[const_trait] trait NonConst {}
38+
| ++++++++++++++
39+
40+
error: `[const]` can only be applied to `#[const_trait]` traits
41+
--> $DIR/const-trait-bounds-trait-objects.rs:18:19
42+
|
43+
LL | const fn take(_: &dyn [const] NonConst) {}
44+
| ^^^^^^^^^^^ can't be applied to `NonConst`
45+
|
46+
help: mark `NonConst` as `#[const_trait]` to allow it to have `const` implementations
47+
|
48+
LL | #[const_trait] trait NonConst {}
49+
| ++++++++++++++
50+
51+
error: aborting due to 6 previous errors
3052

0 commit comments

Comments
 (0)