You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot infer const parameter that has a default value #83687
I expected to see this happen:
Code compiles and arr is Array<1>, because of the default.
Instead, this happened:
warning: the feature `const_generics_defaults` is incomplete and may not be safe to use and/or cause compiler crashes
--> infer.rs:1:12
|
1 | #![feature(const_generics_defaults)]
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
error[E0282]: type annotations needed for `&Array<{_: usize}>`
--> infer.rs:13:15
|
13 | let arr = Array::from_slice(&slice);
| --- ^^^^^ cannot infer the value of const parameter `N` declared on the struct `Array`
| |
| consider giving `arr` the explicit type `&Array<{_: usize}>`, where the type parameter `N` is specified
|
help: consider specifying the const argument
|
13 | let arr = Array::<N>::from_slice(&slice);
| ^^^^^^^^^^
error: aborting due to previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0282`.
Clearly, VirtAddr::new gets de-sugared into VirtAddr::<()>::new
That is actually not true. Since the associated function new is defined for VirtAddr<T> for all types T: Sized, the subexpression VirtAddr::new in the third from last line is treated as <VirtAddr<?T>>::new by the type checker where ?T denotes an inference variable. In the type annotation on the other hand, VirtAddr expands to VirtAddr<()> due to the default argument.
As a result, we have VirtAddr<()> on the left side and VirtAddr<?T> on the right side (ignoring the call to new here).
During unification, the type checker gets to know that ?T == () and thus everything works out.
Only if you were to write <VirtAddr>::new, it would get resolved to <VirtAddr<()>>::new.
Activity
fmease commentedon Mar 31, 2021
Default type/const parameters are not taken into consideration during type inference. This is an already known flaw. See a Reddit comment of mine.
I think this is a duplicate of #27336
Soveu commentedon Apr 16, 2021
Lets take for example this code
Clearly,
VirtAddr::newgets de-sugared intoVirtAddr::<()>::new, so why can't hereArray::from_slicede-sugar intoArray::<1usize>::from_slice?EDIT: Actually, just leaving
let a = VirtAddr::new(std::ptr::null())withoutlet b: VirtAddr<()> = adoes not work either 🤔
Soveu commentedon Apr 16, 2021
I guess I will close this issue as this is consistent with other behavior
fmease commentedon Apr 18, 2021
That is actually not true. Since the associated function
newis defined forVirtAddr<T>for all typesT: Sized, the subexpressionVirtAddr::newin the third from last line is treated as<VirtAddr<?T>>::newby the type checker where?Tdenotes an inference variable. In the type annotation on the other hand,VirtAddrexpands toVirtAddr<()>due to the default argument.As a result, we have
VirtAddr<()>on the left side andVirtAddr<?T>on the right side (ignoring the call tonewhere).During unification, the type checker gets to know that
?T == ()and thus everything works out.Only if you were to write
<VirtAddr>::new, it would get resolved to<VirtAddr<()>>::new.