Skip to content

Fix a bug preventing const parameters from being used in const generic impls #60717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,18 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
ty,
};

let expr = &tcx.hir().body(ast_const.body).value;
let mut expr = &tcx.hir().body(ast_const.body).value;

// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
if let ExprKind::Block(block, _) = &expr.node {
if block.stmts.is_empty() {
if let Some(trailing) = &block.expr {
expr = &trailing;
}
}
}

if let ExprKind::Path(ref qpath) = expr.node {
if let hir::QPath::Resolved(_, ref path) = qpath {
if let Res::Def(DefKind::ConstParam, def_id) = path.res {
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/const-generics/impl-const-generic-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// run-pass

#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash

struct S<const X: u32>;

impl<const X: u32> S<{X}> {
fn x() -> u32 {
X
}
}

fn main() {
assert_eq!(S::<19>::x(), 19);
}
6 changes: 6 additions & 0 deletions src/test/ui/const-generics/impl-const-generic-struct.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/impl-const-generic-struct.rs:3:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^