Skip to content

Commit 083c8c9

Browse files
committed
HIR ty lowering: Simplify signature of lower_poly_trait_ref
1 parent 4e97337 commit 083c8c9

File tree

7 files changed

+80
-34
lines changed

7 files changed

+80
-34
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: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

tests/ui/traits/const-traits/mutually-exclusive-trait-bound-modifiers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
const fn maybe_const_maybe<T: [const] ?Sized>() {}
44
//~^ ERROR `[const]` trait not allowed with `?` trait polarity modifier
5+
//~| ERROR `[const]` can only be applied to `#[const_trait]` traits
6+
//~| ERROR `[const]` can only be applied to `#[const_trait]` traits
57

68
fn const_maybe<T: const ?Sized>() {}
79
//~^ ERROR `const` trait not allowed with `?` trait polarity modifier
10+
//~| ERROR `const` can only be applied to `#[const_trait]` traits
811

912
const fn maybe_const_negative<T: [const] !Trait>() {}
1013
//~^ ERROR `[const]` trait not allowed with `!` trait polarity modifier

tests/ui/traits/const-traits/mutually-exclusive-trait-bound-modifiers.stderr

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,68 @@ LL | const fn maybe_const_maybe<T: [const] ?Sized>() {}
77
| there is not a well-defined meaning for a `[const] ?` trait
88

99
error: `const` trait not allowed with `?` trait polarity modifier
10-
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:6:25
10+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:8:25
1111
|
1212
LL | fn const_maybe<T: const ?Sized>() {}
1313
| ----- ^
1414
| |
1515
| there is not a well-defined meaning for a `const ?` trait
1616

1717
error: `[const]` trait not allowed with `!` trait polarity modifier
18-
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:9:42
18+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:12:42
1919
|
2020
LL | const fn maybe_const_negative<T: [const] !Trait>() {}
2121
| ------- ^
2222
| |
2323
| there is not a well-defined meaning for a `[const] !` trait
2424

2525
error: `const` trait not allowed with `!` trait polarity modifier
26-
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:13:28
26+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:16:28
2727
|
2828
LL | fn const_negative<T: const !Trait>() {}
2929
| ----- ^
3030
| |
3131
| there is not a well-defined meaning for a `const !` trait
3232

3333
error: negative bounds are not supported
34-
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:9:42
34+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:12:42
3535
|
3636
LL | const fn maybe_const_negative<T: [const] !Trait>() {}
3737
| ^
3838

3939
error: negative bounds are not supported
40-
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:13:28
40+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:16:28
4141
|
4242
LL | fn const_negative<T: const !Trait>() {}
4343
| ^
4444

45-
error: aborting due to 6 previous errors
45+
error: `[const]` can only be applied to `#[const_trait]` traits
46+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:3:31
47+
|
48+
LL | const fn maybe_const_maybe<T: [const] ?Sized>() {}
49+
| ^^^^^^^ can't be applied to `Sized`
50+
|
51+
note: `Sized` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
52+
--> $SRC_DIR/core/src/marker.rs:LL:COL
53+
54+
error: `[const]` can only be applied to `#[const_trait]` traits
55+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:3:31
56+
|
57+
LL | const fn maybe_const_maybe<T: [const] ?Sized>() {}
58+
| ^^^^^^^ can't be applied to `Sized`
59+
|
60+
note: `Sized` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
61+
--> $SRC_DIR/core/src/marker.rs:LL:COL
62+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
63+
64+
error: `const` can only be applied to `#[const_trait]` traits
65+
--> $DIR/mutually-exclusive-trait-bound-modifiers.rs:8:19
66+
|
67+
LL | fn const_maybe<T: const ?Sized>() {}
68+
| ^^^^^ can't be applied to `Sized`
69+
|
70+
note: `Sized` can't be used with `const` because it isn't annotated with `#[const_trait]`
71+
--> $SRC_DIR/core/src/marker.rs:LL:COL
72+
73+
error: aborting due to 9 previous errors
4674

0 commit comments

Comments
 (0)