hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.27k stars 225 forks source link

[BUG] Missing `cpp2` namespace qualifier in lowered Cpp1 when writing `unique.new` or `shared.new` #939

Closed bluetarpmedia closed 6 months ago

bluetarpmedia commented 6 months ago

Describe the bug Cppfront is not emitting the cpp2 namespace qualifier on unique or shared when writing unique.new or shared.new. I seem to remember this working a long time ago so I wonder if UFCS refactoring has affected it.

To Reproduce Run cppfront on this code:

main: () -> int = {
    up: = unique.new<int>(123);
    sp: = shared.new<int>(456);
    up.reset();
    sp.reset();
    return 0;
}

This lowers to:

[[nodiscard]] auto main() -> int{
    auto up {CPP2_UFCS_TEMPLATE(cpp2_new<int>)(unique, 123)}; 
    auto sp {CPP2_UFCS_TEMPLATE(cpp2_new<int>)(shared, 456)}; 
    CPP2_UFCS(reset)(std::move(up));
    CPP2_UFCS(reset)(std::move(sp));
    return 0; 
}

Then compile with a C++ compiler. It will report an error along the lines of:

use of undeclared identifier 'unique'; did you mean 'cpp2::unique'?

See Godbolt

Additional context Regular new<int> works correctly. It's only unique.new or shared.new that has a problem. I couldn't find any regression tests with unique.new or shared.new so that's probably worth adding as part of the fix for this.

hsutter commented 6 months ago

Thanks! I think it's still a tossup whether this special case is worth saving people writing cpp2::, but I think the principle does apply that there's nothing else the programmer could mean given that new is a Cpp1 reserved word.