Skip to content

Commit cd2edf6

Browse files
committed
Add CosntnessArg::Const when needed
1 parent 84054f7 commit cd2edf6

File tree

6 files changed

+70
-10
lines changed

6 files changed

+70
-10
lines changed

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
206206
// methods in `subst.rs`, so that we can iterate over the arguments and
207207
// parameters in lock-step linearly, instead of trying to match each pair.
208208
let mut substs: SmallVec<[subst::GenericArg<'tcx>; 8]> = SmallVec::with_capacity(count);
209+
let mut has_constness = false;
209210
// Iterate over each segment of the path.
210211
while let Some((def_id, defs)) = stack.pop() {
211212
let mut params = defs.params.iter().peekable();
@@ -219,6 +220,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
219220
} else {
220221
substs.push(kind);
221222
}
223+
if let subst::GenericArgKind::Constness(_) = kind.unpack() {
224+
has_constness = true;
225+
}
222226
params.next();
223227
} else {
224228
break;
@@ -386,7 +390,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
386390
}
387391
}
388392
}
389-
393+
if !has_constness && has_self && let Some(constness) = constness {
394+
if let ty::ConstnessArg::Const = constness {
395+
substs.push(constness.into());
396+
}
397+
}
390398
tcx.intern_substs(&substs)
391399
}
392400

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
574574
.unwrap_or_else(|| match self.astconv.item_def_id() {
575575
// no information available
576576
// TODO: fall back to `Not`?
577-
None => if infer_args { ty::ConstnessArg::Infer } else { ty::ConstnessArg::Not },
577+
None => {
578+
trace!("uh oh");
579+
if infer_args {
580+
ty::ConstnessArg::Infer
581+
} else {
582+
ty::ConstnessArg::Not
583+
}
584+
}
578585
Some(context) => {
579586
if tcx.generics_of(context).has_constness_param() {
580587
ty::ConstnessArg::Const

src/test/ui/rfc-2632-const-trait-impl/const-trait-bound.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ const fn foo<T: ~const Foo>() {
1010
T::foo();
1111
}
1212

13-
1413
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
#![feature(const_trait_impl)]
3+
4+
#[const_trait]
5+
trait Foo {
6+
fn foo();
7+
}
8+
9+
pub struct W<T>(T);
10+
11+
impl<T: ~const Foo> const Foo for W<T> {
12+
fn foo() {
13+
<T as Foo>::foo();
14+
}
15+
}
16+
17+
fn main() {}

src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ impl T for S {}
77

88
fn rpit() -> impl ~const T { S }
99
//~^ ERROR `~const` is not allowed
10+
//~| ERROR the trait bound
1011

1112
fn apit(_: impl ~const T) {}
1213
//~^ ERROR `~const` is not allowed
1314

1415
fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
1516
//~^ ERROR `~const` is not allowed
17+
//~| ERROR the trait bound
1618

1719
fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
1820
//~^ ERROR `~const` is not allowed

src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,77 @@ LL | fn rpit() -> impl ~const T { S }
77
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
88

99
error: `~const` is not allowed here
10-
--> $DIR/tilde-const-invalid-places.rs:11:17
10+
--> $DIR/tilde-const-invalid-places.rs:12:17
1111
|
1212
LL | fn apit(_: impl ~const T) {}
1313
| ^^^^^^^^
1414
|
1515
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
1616

1717
error: `~const` is not allowed here
18-
--> $DIR/tilde-const-invalid-places.rs:14:50
18+
--> $DIR/tilde-const-invalid-places.rs:15:50
1919
|
2020
LL | fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
2121
| ^^^^^^^^
2222
|
2323
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
2424

2525
error: `~const` is not allowed here
26-
--> $DIR/tilde-const-invalid-places.rs:17:48
26+
--> $DIR/tilde-const-invalid-places.rs:19:48
2727
|
2828
LL | fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
2929
| ^^^^^^^^
3030
|
3131
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
3232

3333
error: `~const` is not allowed here
34-
--> $DIR/tilde-const-invalid-places.rs:20:15
34+
--> $DIR/tilde-const-invalid-places.rs:22:15
3535
|
3636
LL | fn generic<P: ~const T>() {}
3737
| ^^^^^^^^
3838
|
3939
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
4040

4141
error: `~const` is not allowed here
42-
--> $DIR/tilde-const-invalid-places.rs:23:31
42+
--> $DIR/tilde-const-invalid-places.rs:25:31
4343
|
4444
LL | fn where_clause<P>() where P: ~const T {}
4545
| ^^^^^^^^
4646
|
4747
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
4848

4949
error: `~const` and `?` are mutually exclusive
50-
--> $DIR/tilde-const-invalid-places.rs:26:25
50+
--> $DIR/tilde-const-invalid-places.rs:28:25
5151
|
5252
LL | struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
5353
| ^^^^^^^^^^^^^
5454

55-
error: aborting due to 7 previous errors
55+
error[E0277]: the trait bound `S: T` is not satisfied
56+
--> $DIR/tilde-const-invalid-places.rs:8:14
57+
|
58+
LL | fn rpit() -> impl ~const T { S }
59+
| ^^^^^^^^^^^^^ the trait `T` is not implemented for `S`
60+
|
61+
note: the trait `T` is implemented for `S`, but that implementation is not `const`
62+
--> $DIR/tilde-const-invalid-places.rs:8:14
63+
|
64+
LL | fn rpit() -> impl ~const T { S }
65+
| ^^^^^^^^^^^^^
66+
= help: the trait `T` is implemented for `S`
67+
68+
error[E0277]: the trait bound `S: T` is not satisfied
69+
--> $DIR/tilde-const-invalid-places.rs:15:26
70+
|
71+
LL | fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
72+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `T` is not implemented for `S`
73+
|
74+
note: the trait `T` is implemented for `S`, but that implementation is not `const`
75+
--> $DIR/tilde-const-invalid-places.rs:15:26
76+
|
77+
LL | fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
= help: the trait `T` is implemented for `S`
80+
81+
error: aborting due to 9 previous errors
5682

83+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)