Skip to content

Commit b2c957a

Browse files
committed
fix the issue that typenameof does not return the correct type name for template parameters.
1 parent 5d33da4 commit b2c957a

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

lib/source/pl/core/ast/ast_node_type_operator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ namespace pl::core::ast {
5858
break;
5959
case Token::Operator::TypeNameOf: {
6060
if (auto typeApp = dynamic_cast<ASTNodeTypeApplication*>(this->m_expression.get()); typeApp != nullptr) {
61-
result = typeApp->getTypeName();
61+
auto evaluatedType = typeApp->evaluate(evaluator);
62+
result = dynamic_cast<ASTNodeTypeApplication*>(evaluatedType.get())->getTypeName();
6263
} else {
6364
result = pattern->getTypeName();
6465
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(AVAILABLE_TESTS
3333
Format
3434
RValuesAssignmentInStruct
3535
TemplateParametersScope
36+
TypeNameOf
3637
)
3738

3839

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include "test_pattern.hpp"
4+
5+
namespace pl::test {
6+
7+
class TestPatternTypeNameOf : public TestPattern {
8+
public:
9+
TestPatternTypeNameOf(core::Evaluator *evaluator) : TestPattern(evaluator, "TypeNameOf") {
10+
}
11+
~TestPatternTypeNameOf() override = default;
12+
13+
[[nodiscard]] std::string getSourceCode() const override {
14+
return R"(
15+
struct A {
16+
17+
};
18+
19+
struct B<T, auto y> {
20+
21+
};
22+
23+
struct TypeName <type, auto typename> {
24+
std::assert(typenameof(type) == typename, "type name should match");
25+
};
26+
27+
u32 P = 16;
28+
TypeName<u32, "u32"> a @ 0;
29+
TypeName<A, "A"> b @ 0;
30+
TypeName<B<u32, 2>, "B<u32, 2>"> c @ 0;
31+
TypeName<B<B<u32, P>, 2>, "B<B<u32, 16>, 2>"> d @ 0;
32+
TypeName<TypeName<A, "A"> , "TypeName<A, \"A\">"> e @ 0;
33+
std::assert(typenameof(B<B<u32, P>, 2>) == "B<B<u32, 16>, 2>", "type name should match");
34+
)";
35+
}
36+
};
37+
38+
}

tests/source/tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "test_patterns/test_pattern_format.hpp"
2929
#include "test_patterns/test_pattern_rvalues_assignment_in_struct.hpp"
3030
#include "test_patterns/test_pattern_template_parameters_scope.hpp"
31+
#include "test_patterns/test_pattern_typenameof.hpp"
3132

3233
static pl::core::Evaluator s_evaluator;
3334

@@ -61,4 +62,5 @@ std::array Tests = {
6162
TEST(Format),
6263
TEST(RValuesAssignmentInStruct),
6364
TEST(TemplateParametersScope),
65+
TEST(TypeNameOf),
6466
};

0 commit comments

Comments
 (0)