Skip to content

Commit 2396a15

Browse files
authored
Rollup merge of rust-lang#61805 - davidtwco:ice-const-generic-repeat-expr-count-sequel, r=varkor
typeck: Fix ICE for blocks in repeat expr count. Fixes rust-lang#61336 (again). This PR fixes an ICE that occured when a block expression resolving to a const generic was used for the count of an array repeat expression. r? @varkor
2 parents 75d136e + bc36aab commit 2396a15

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

src/librustc_typeck/astconv.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21572157

21582158
/// Returns the `DefId` of the constant parameter that the provided expression is a path to.
21592159
pub fn const_param_def_id(&self, expr: &hir::Expr) -> Option<DefId> {
2160+
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2161+
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
2162+
let expr = match &expr.node {
2163+
ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() =>
2164+
block.expr.as_ref().unwrap(),
2165+
_ => expr,
2166+
};
2167+
21602168
match &expr.node {
21612169
ExprKind::Path(hir::QPath::Resolved(_, path)) => match path.res {
21622170
Res::Def(DefKind::ConstParam, did) => Some(did),
@@ -2184,18 +2192,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21842192
ty,
21852193
};
21862194

2187-
let mut expr = &tcx.hir().body(ast_const.body).value;
2188-
2189-
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2190-
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
2191-
if let ExprKind::Block(block, _) = &expr.node {
2192-
if block.stmts.is_empty() {
2193-
if let Some(trailing) = &block.expr {
2194-
expr = &trailing;
2195-
}
2196-
}
2197-
}
2198-
2195+
let expr = &tcx.hir().body(ast_const.body).value;
21992196
if let Some(def_id) = self.const_param_def_id(expr) {
22002197
// Find the name and index of the const parameter by indexing the generics of the
22012198
// parent item and construct a `ParamConst`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
3+
4+
fn f<T: Copy, const N: usize>(x: T) -> [T; N] {
5+
[x; {N}]
6+
}
7+
8+
fn g<T, const N: usize>(x: T) -> [T; N] {
9+
[x; {N}]
10+
//~^ ERROR the trait bound `T: std::marker::Copy` is not satisfied [E0277]
11+
}
12+
13+
fn main() {
14+
let x: [u32; 5] = f::<u32, 5>(3);
15+
assert_eq!(x, [3u32; 5]);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/issue-61336-2.rs:1:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
7+
error[E0277]: the trait bound `T: std::marker::Copy` is not satisfied
8+
--> $DIR/issue-61336-2.rs:9:5
9+
|
10+
LL | [x; {N}]
11+
| ^^^^^^^^ the trait `std::marker::Copy` is not implemented for `T`
12+
|
13+
= help: consider adding a `where T: std::marker::Copy` bound
14+
= note: the `Copy` trait is required because the repeated element will be copied
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)