Skip to content

Commit

Permalink
Prevent type parameter shadowing of builtin types
Browse files Browse the repository at this point in the history
When a type parameter is introduced which matches any of the simple UIF
names, it causes any other such simple type to be recognized as a type
parameter reference, which results in the compiler aborting.

In the discussion on ponylang#1526, it was decided that all
shadowing by type parameters should be prohibited.

Hence, in this patch, if we're resolving a type param whose id is still
nominal, we check the nearest module to see if that name is already
defined.
  • Loading branch information
tokenrove committed Jan 24, 2017
1 parent a58da60 commit 235cc4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/libponyc/pass/names.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ static bool names_typeparam(pass_opt_t* opt, ast_t** astp, ast_t* def)
return false;
}

if (ast_id(ast) == TK_NOMINAL) {
ast_t* module = ast_nearest(ast, TK_MODULE);
if (module && ast_get(module, ast_name(id), 0)) {
ast_error(opt->check.errors, def,
"Type parameter shadows existing type");
return false;
}
}

// Change to a typeparamref.
REPLACE(astp,
NODE(TK_TYPEPARAMREF,
Expand Down
9 changes: 9 additions & 0 deletions test/libponyc/badpony.cc
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,12 @@ TEST_F(BadPonyTest, MatchUnionOfDifferentCaps)
TEST_ERRORS_1(src,
"match type may not be a union of types with different capabilities");
}

TEST_F(BadPonyTest, ShadowingBuiltinTypeParameter)
{
const char* src =
"class A[I8]"
"let b: U8 = 0";

TEST_ERRORS_1(src, "Type parameter shadows existing type");
}

0 comments on commit 235cc4f

Please sign in to comment.