Skip to content

Inline const blocks default integers to i32 #78132

Closed
@DutchGhost

Description

@DutchGhost

Inline const blocks default to the i32 type on integer literals, see the following:

#![feature(inline_const)]

fn main() {
    let _: usize = const { 0 };
}

fails compilation with:

error[E0308]: mismatched types
 --> src/main.rs:4:20
  |
4 |     let _: usize = const { 0 };
  |            -----   ^^^^^ expected `usize`, found `i32`
  |            |
  |            expected due to this
  |
help: you can convert an `i32` to `usize` and panic if the converted value wouldn't fit
  |
4 |     let _: usize = const.try_into().unwrap() { 0 };

Regular integer literals, and in unsafe blocks just get the type right: let _: usize = 0 and let _: usize = unsafe { 0 };

Another minor thing is the error message, const.try_into().unwrap() { 0 }; is an invalid suggestion ^^.

Metadata

Metadata

Assignees

Labels

A-inferenceArea: Type inferenceC-bugCategory: This is a bug.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions