Closed
Description
I tried this code:
#![feature(const_generics)]
#![allow(incomplete_features)]
use std::fmt;
const fn max(a: usize, b: usize) -> usize {
[a, b][(a < b) as usize]
}
pub struct Foo<const N: usize>;
impl<const N: usize> fmt::Debug for Foo<N> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "Foo({:?})", N)
}
}
// ok
fn bar_let<const N: usize, const M: usize>(a: Foo<N>, b: Foo<M>) {
let a = Foo::<{ max(N, M) }>;
println!("{:?}", a);
}
// // fail to compile
fn bar_ret<const N: usize, const M: usize>(a: Foo<N>, b: Foo<M>) -> Foo<{ max(N, M) }> {
let a = Foo::<{ max(N, M) }>;
println!("{:?}", a);
a
}
fn main() {
let a = Foo::<{ max(2, 3) }>;
println!("{:?}", a);
bar_let(Foo::<5>, Foo::<3>);
let b = bar_ret(Foo::<5>, Foo::<3>);
}
I expected to see this happen:
compile success
Instead, this happened:
error[E0308]: mismatched types
--> src/main.rs:27:5
|
24 | fn bar_ret<const N: usize, const M: usize>(a: Foo<{ N }>, b: Foo<{ M }>) -> Foo<{ max(N, M) }> {
| ------------------ expected `Foo<{ max(N, M) }>` because of return type
...
27 | a
| ^ expected `{ max(N, M) }`, found `{ max(N, M) }`
|
= note: expected struct `Foo<{ max(N, M) }>`
found struct `Foo<{ max(N, M) }>`
Meta
rustc --version --verbose
:
rustc 1.43.0-nightly (564758c4c 2020-03-08)
binary: rustc
commit-hash: 564758c4c329e89722454dd2fbb35f1ac0b8b47c
commit-date: 2020-03-08
host: x86_64-apple-darwin
release: 1.43.0-nightly
LLVM version: 9.0
Backtrace
<backtrace>
Metadata
Metadata
Assignees
Labels
Area: const generics (parameters and arguments)Area: Messages for errors, warnings, and lintsCategory: This is a bug.Diagnostics: Confusing error or lint that should be reworked.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.Relevant to the compiler team, which will review and decide on the PR/issue.This issue requires a nightly compiler in some way.