Skip to content

Commit fbecda7

Browse files
committed
HIR ty lowering: Simplify signature of lower_poly_trait_ref
1 parent 1bb3352 commit fbecda7

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
@@ -480,12 +480,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
480480

481481
match hir_bound {
482482
hir::GenericBound::Trait(poly_trait_ref) => {
483-
let hir::TraitBoundModifiers { constness, polarity } = poly_trait_ref.modifiers;
484483
let _ = self.lower_poly_trait_ref(
485-
&poly_trait_ref.trait_ref,
486-
poly_trait_ref.span,
487-
constness,
488-
polarity,
484+
poly_trait_ref,
489485
param_ty,
490486
bounds,
491487
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
@@ -17,9 +17,7 @@ use tracing::{debug, instrument};
1717

1818
use super::HirTyLowerer;
1919
use crate::errors::SelfInTypeAlias;
20-
use crate::hir_ty_lowering::{
21-
GenericArgCountMismatch, GenericArgCountResult, PredicateFilter, RegionInferReason,
22-
};
20+
use crate::hir_ty_lowering::{GenericArgCountMismatch, PredicateFilter, RegionInferReason};
2321

2422
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2523
/// Lower a trait object type from the HIR to our internal notion of a type.
@@ -37,24 +35,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
3735

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

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -735,17 +735,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
735735
/// If for example you had `for<'a> Foo<'a>: Bar<'a>`, then the `self_ty` would be `Foo<'a>`
736736
/// where `'a` is a bound region at depth 0. Similarly, the `trait_ref` would be `Bar<'a>`.
737737
/// The lowered poly-trait-ref will track this binder explicitly, however.
738-
#[instrument(level = "debug", skip(self, span, constness, bounds))]
738+
#[instrument(level = "debug", skip(self, bounds))]
739739
pub(crate) fn lower_poly_trait_ref(
740740
&self,
741-
trait_ref: &hir::TraitRef<'tcx>,
742-
span: Span,
743-
constness: hir::BoundConstness,
744-
polarity: hir::BoundPolarity,
741+
poly_trait_ref: &hir::PolyTraitRef<'tcx>,
745742
self_ty: Ty<'tcx>,
746743
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
747744
predicate_filter: PredicateFilter,
748745
) -> GenericArgCountResult {
746+
// We use the *resolved* bound vars later instead of the HIR ones since the former
747+
// also include the bound vars of the overarching predicate if applicable.
748+
let hir::PolyTraitRef { bound_generic_params: _, modifiers, ref trait_ref, span } =
749+
*poly_trait_ref;
750+
let hir::TraitBoundModifiers { constness, polarity } = modifiers;
751+
749752
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
750753
let trait_segment = trait_ref.path.segments.last().unwrap();
751754

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:23
22+
--> $DIR/const-trait-bounds-trait-objects.rs:18:23
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:23
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)