Skip to content

Commit be56470

Browse files
committed
Check for element being const before resolving repeat count
1 parent 9a28568 commit be56470

File tree

3 files changed

+65
-80
lines changed

3 files changed

+65
-80
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+47-48
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
115115
let deferred_repeat_expr_checks = deferred_repeat_expr_checks
116116
.drain(..)
117117
.flat_map(|(element, element_ty, count)| {
118+
// Actual constants as the repeat element get inserted repeatedly instead of getting copied via Copy
119+
// so we don't need to attempt to structurally resolve the repeat count which may unnecessarily error.
120+
match &element.kind {
121+
hir::ExprKind::ConstBlock(..) => return None,
122+
hir::ExprKind::Path(qpath) => {
123+
let res = self.typeck_results.borrow().qpath_res(qpath, element.hir_id);
124+
if let Res::Def(
125+
DefKind::Const | DefKind::AssocConst | DefKind::AnonConst,
126+
_,
127+
) = res
128+
{
129+
return None;
130+
}
131+
}
132+
_ => {}
133+
}
134+
118135
// We want to emit an error if the const is not structurally resolveable as otherwise
119136
// we can find up conservatively proving `Copy` which may infer the repeat expr count
120137
// to something that never required `Copy` in the first place.
@@ -135,12 +152,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
135152
// expr's `Copy` check.
136153
.collect::<Vec<_>>();
137154

155+
let enforce_copy_bound = |element: &hir::Expr<'_>, element_ty| {
156+
// If someone calls a const fn or constructs a const value, they can extract that
157+
// out into a separate constant (or a const block in the future), so we check that
158+
// to tell them that in the diagnostic. Does not affect typeck.
159+
let is_constable = match element.kind {
160+
hir::ExprKind::Call(func, _args) => match *self.node_ty(func.hir_id).kind() {
161+
ty::FnDef(def_id, _) if self.tcx.is_stable_const_fn(def_id) => {
162+
traits::IsConstable::Fn
163+
}
164+
_ => traits::IsConstable::No,
165+
},
166+
hir::ExprKind::Path(qpath) => {
167+
match self.typeck_results.borrow().qpath_res(&qpath, element.hir_id) {
168+
Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => traits::IsConstable::Ctor,
169+
_ => traits::IsConstable::No,
170+
}
171+
}
172+
_ => traits::IsConstable::No,
173+
};
174+
175+
let lang_item = self.tcx.require_lang_item(LangItem::Copy, None);
176+
let code = traits::ObligationCauseCode::RepeatElementCopy {
177+
is_constable,
178+
elt_span: element.span,
179+
};
180+
self.require_type_meets(element_ty, element.span, code, lang_item);
181+
};
182+
138183
for (element, element_ty, count) in deferred_repeat_expr_checks {
139184
match count.kind() {
140185
ty::ConstKind::Value(val)
141186
if val.try_to_target_usize(self.tcx).is_none_or(|count| count > 1) =>
142187
{
143-
self.enforce_repeat_element_needs_copy_bound(element, element_ty)
188+
enforce_copy_bound(element, element_ty)
144189
}
145190
// If the length is 0 or 1 we don't actually copy the element, we either don't create it
146191
// or we just use the one value.
@@ -151,9 +196,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
151196
ty::ConstKind::Param(_)
152197
| ty::ConstKind::Expr(_)
153198
| ty::ConstKind::Placeholder(_)
154-
| ty::ConstKind::Unevaluated(_) => {
155-
self.enforce_repeat_element_needs_copy_bound(element, element_ty)
156-
}
199+
| ty::ConstKind::Unevaluated(_) => enforce_copy_bound(element, element_ty),
157200

158201
ty::ConstKind::Bound(_, _) | ty::ConstKind::Infer(_) | ty::ConstKind::Error(_) => {
159202
unreachable!()
@@ -162,50 +205,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
162205
}
163206
}
164207

165-
/// Requires that `element_ty` is `Copy` (unless it's a const expression itself).
166-
pub(super) fn enforce_repeat_element_needs_copy_bound(
167-
&self,
168-
element: &hir::Expr<'_>,
169-
element_ty: Ty<'tcx>,
170-
) {
171-
// Actual constants as the repeat element get inserted repeatedly instead of getting copied via Copy.
172-
match &element.kind {
173-
hir::ExprKind::ConstBlock(..) => return,
174-
hir::ExprKind::Path(qpath) => {
175-
let res = self.typeck_results.borrow().qpath_res(qpath, element.hir_id);
176-
if let Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::AnonConst, _) = res
177-
{
178-
return;
179-
}
180-
}
181-
_ => {}
182-
}
183-
184-
// If someone calls a const fn or constructs a const value, they can extract that
185-
// out into a separate constant (or a const block in the future), so we check that
186-
// to tell them that in the diagnostic. Does not affect typeck.
187-
let is_constable = match element.kind {
188-
hir::ExprKind::Call(func, _args) => match *self.node_ty(func.hir_id).kind() {
189-
ty::FnDef(def_id, _) if self.tcx.is_stable_const_fn(def_id) => {
190-
traits::IsConstable::Fn
191-
}
192-
_ => traits::IsConstable::No,
193-
},
194-
hir::ExprKind::Path(qpath) => {
195-
match self.typeck_results.borrow().qpath_res(&qpath, element.hir_id) {
196-
Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => traits::IsConstable::Ctor,
197-
_ => traits::IsConstable::No,
198-
}
199-
}
200-
_ => traits::IsConstable::No,
201-
};
202-
203-
let lang_item = self.tcx.require_lang_item(LangItem::Copy, None);
204-
let code =
205-
traits::ObligationCauseCode::RepeatElementCopy { is_constable, elt_span: element.span };
206-
self.require_type_meets(element_ty, element.span, code, lang_item);
207-
}
208-
209208
pub(in super::super) fn check_method_argument_types(
210209
&self,
211210
sp: Span,

tests/ui/repeat-expr/copy-check-const-element-uninferred-count.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ fn tie_and_make_goal<const N: usize, T: Trait<N>>(_: &T, _: &[String; N]) {}
2020
fn const_block() {
2121
// Deferred repeat expr `String; ?n`
2222
let a = [const { String::new() }; _];
23-
//~^ ERROR: type annotations needed for `[String; _]`
2423

2524
// `?int: Trait<?n>` goal
2625
tie_and_make_goal(&1, &a);
@@ -36,7 +35,6 @@ fn const_item() {
3635

3736
// Deferred repeat expr `String; ?n`
3837
let a = [MY_CONST; _];
39-
//~^ ERROR: type annotations needed for `[String; _]`
4038

4139
// `?int: Trait<?n>` goal
4240
tie_and_make_goal(&1, &a);
@@ -54,12 +52,21 @@ fn assoc_const() {
5452

5553
// Deferred repeat expr `String; ?n`
5654
let a = [<() as Dummy>::ASSOC; _];
57-
//~^ ERROR: type annotations needed for `[String; _]`
5855

5956
// `?int: Trait<?n>` goal
6057
tie_and_make_goal(&1, &a);
6158

6259
// ... same as `const_block`
6360
}
6461

62+
fn const_block_but_uninferred() {
63+
// Deferred repeat expr `String; ?n`
64+
let a = [const { String::new() }; _];
65+
//~^ ERROR: type annotations needed for `[String; _]`
66+
67+
// Even if we don't structurally resolve the repeat count as part of repeat expr
68+
// checks, we still error on the repeat count being uninferred as we require all
69+
// types/consts to be inferred by the end of type checking.
70+
}
71+
6572
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
1-
error[E0282]: type annotations needed for `[String; _]`
2-
--> $DIR/copy-check-const-element-uninferred-count.rs:22:9
1+
error[E0284]: type annotations needed for `[String; _]`
2+
--> $DIR/copy-check-const-element-uninferred-count.rs:64:9
33
|
44
LL | let a = [const { String::new() }; _];
5-
| ^ ----------------------- type must be known at this point
5+
| ^ ---------------------------- type must be known at this point
66
|
7-
help: consider giving `a` an explicit type, where the value of const parameter `N` is specified
7+
= note: the length of array `[String; _]` must be type `usize`
8+
help: consider giving `a` an explicit type, where the placeholders `_` are specified
89
|
9-
LL | let a: [_; N] = [const { String::new() }; _];
10+
LL | let a: [_; _] = [const { String::new() }; _];
1011
| ++++++++
1112

12-
error[E0282]: type annotations needed for `[String; _]`
13-
--> $DIR/copy-check-const-element-uninferred-count.rs:38:9
14-
|
15-
LL | let a = [MY_CONST; _];
16-
| ^ -------- type must be known at this point
17-
|
18-
help: consider giving `a` an explicit type, where the value of const parameter `N` is specified
19-
|
20-
LL | let a: [_; N] = [MY_CONST; _];
21-
| ++++++++
22-
23-
error[E0282]: type annotations needed for `[String; _]`
24-
--> $DIR/copy-check-const-element-uninferred-count.rs:56:9
25-
|
26-
LL | let a = [<() as Dummy>::ASSOC; _];
27-
| ^ -------------------- type must be known at this point
28-
|
29-
help: consider giving `a` an explicit type, where the value of const parameter `N` is specified
30-
|
31-
LL | let a: [_; N] = [<() as Dummy>::ASSOC; _];
32-
| ++++++++
33-
34-
error: aborting due to 3 previous errors
13+
error: aborting due to 1 previous error
3514

36-
For more information about this error, try `rustc --explain E0282`.
15+
For more information about this error, try `rustc --explain E0284`.

0 commit comments

Comments
 (0)