Skip to content

[BUG] Cannot handle assignment to u8 and i16 from literal values #1446

@jmafc

Description

@jmafc

Describe the bug
If a variable is declared as u8 and a character literal is assigned to it, or a variable is declared as i16 and an integer literal is assigned to it, the generated code cannot be compiled by clang++ (g++ compiles but with warnings). I suspect this also applies to the respective signed/unsigned counterparts.

To Reproduce
Steps to reproduce the behavior:

  1. Sample code - distilled down to minimal essentials please
main: () -> int = {
   foo: u8;
   foo = 'a';
   bar: i16;
   bar = 3;
}
  1. Command lines including which C++ compiler you are using
cppfront test.cpp2 -p
clang++ -std=c++20 [or 23] -I<path/to/cppfront/include> test.cpp

cppfront compiler v0.8.2 Build B110:2055
Debian clang version 19.1.7 (3+b1)
[also] g++ (Debian 14.2.0-19) 14.2.0

  1. Expected result - what you expected to happen

Clean, no error compilation.

  1. Actual result/error

clang++ fails when trying to expand #define CPP2_FORWARD(x) std::forward<decltype(x)>(x) with the following note:

test.cpp2:3:8: note: in instantiation of function template specialization 'cpp2::impl::deferred_init<unsigned char>::construct<char>' requested here
    3 |    foo.construct('a');

Additional context
Add any other context about the problem here.
The first problem can be bypassed if foo is declared as char (or _schar but that is probably platform dependent). However, the second one remains, even if bar is declared as short, since (AFAIK) there is no way of indicating an integer literal is a short.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions