Skip to content

Commit af39a1f

Browse files
committedMay 11, 2019
Auto merge of #60717 - varkor:impl-const-generic, r=matthewjasper
Fix a bug preventing const parameters from being used in const generic impls Fixes #60712.
2 parents b8e0d0a + b3207d5 commit af39a1f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
 

‎src/librustc_typeck/astconv.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,18 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
19021902
ty,
19031903
};
19041904

1905-
let expr = &tcx.hir().body(ast_const.body).value;
1905+
let mut expr = &tcx.hir().body(ast_const.body).value;
1906+
1907+
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
1908+
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
1909+
if let ExprKind::Block(block, _) = &expr.node {
1910+
if block.stmts.is_empty() {
1911+
if let Some(trailing) = &block.expr {
1912+
expr = &trailing;
1913+
}
1914+
}
1915+
}
1916+
19061917
if let ExprKind::Path(ref qpath) = expr.node {
19071918
if let hir::QPath::Resolved(_, ref path) = qpath {
19081919
if let Res::Def(DefKind::ConstParam, def_id) = path.res {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-pass
2+
3+
#![feature(const_generics)]
4+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
5+
6+
struct S<const X: u32>;
7+
8+
impl<const X: u32> S<{X}> {
9+
fn x() -> u32 {
10+
X
11+
}
12+
}
13+
14+
fn main() {
15+
assert_eq!(S::<19>::x(), 19);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/impl-const-generic-struct.rs:3:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+

0 commit comments

Comments
 (0)
Please sign in to comment.