Skip to content

Commit

Permalink
Fix regression in class parameters with defaults that refer to other …
Browse files Browse the repository at this point in the history
…parameters
  • Loading branch information
MikePopoloski committed Jan 4, 2022
1 parent c5e7a08 commit f90e406
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
7 changes: 4 additions & 3 deletions source/symbols/ClassSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,10 @@ const Type* GenericClassDefSymbol::getSpecializationImpl(
SmallVectorSized<const ConstantValue*, 8> paramValues;
SmallVectorSized<const Type*, 8> typeParams;
for (auto& decl : paramDecls) {
bool isOverriden = false;
bool anyErrors = false;
auto& param = paramBuilder.createParam(decl, instanceLoc, forceInvalidParams, isForDefault,
anyErrors);
isOverriden, anyErrors);

if (anyErrors) {
if (isForDefault)
Expand All @@ -779,7 +780,7 @@ const Type* GenericClassDefSymbol::getSpecializationImpl(

if (sym.kind == SymbolKind::Parameter) {
auto& ps = sym.as<ParameterSymbol>();
if (!forceInvalidParams)
if (!forceInvalidParams && isOverriden)
ps.getDeclaredType()->resolveAt(context);

if (!param.isLocalParam()) {
Expand All @@ -788,7 +789,7 @@ const Type* GenericClassDefSymbol::getSpecializationImpl(
}
else {
auto& tps = sym.as<TypeParameterSymbol>();
if (!forceInvalidParams)
if (!forceInvalidParams && isOverriden)
tps.targetType.forceResolveAt(context);

if (!param.isLocalParam()) {
Expand Down
9 changes: 5 additions & 4 deletions source/symbols/InstanceSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ InstanceBodySymbol& InstanceBodySymbol::fromDefinition(Compilation& comp,
result->addMembers(*import);

// Add in all parameter ports.
bool unused = false;
bool unused1 = false, unused2 = false;
SmallVectorSized<const ParameterSymbolBase*, 8> params;
auto paramIt = definition.parameters.begin();
while (paramIt != definition.parameters.end()) {
Expand All @@ -570,7 +570,7 @@ InstanceBodySymbol& InstanceBodySymbol::fromDefinition(Compilation& comp,
break;

auto& param = paramBuilder.createParam(decl, instanceLoc, isUninstantiated,
/* suppressErrors */ false, unused);
/* suppressErrors */ false, unused1, unused2);
params.append(&param);
result->addMember(param.symbol);
paramIt++;
Expand All @@ -593,8 +593,9 @@ InstanceBodySymbol& InstanceBodySymbol::fromDefinition(Compilation& comp,
auto& decl = *paramIt;
ASSERT(declarator.name.valueText() == decl.name);

auto& param = paramBuilder.createParam(decl, instanceLoc, isUninstantiated,
/* suppressErrors */ false, unused);
auto& param =
paramBuilder.createParam(decl, instanceLoc, isUninstantiated,
/* suppressErrors */ false, unused1, unused2);
params.append(&param);
result->addMember(param.symbol);
paramIt++;
Expand Down
6 changes: 4 additions & 2 deletions source/symbols/ParameterBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void ParameterBuilder::setAssignments(const ParameterValueAssignmentSyntax& synt
const ParameterSymbolBase& ParameterBuilder::createParam(const Definition::ParameterDecl& decl,
SourceLocation instanceLoc,
bool forceInvalidValues,
bool suppressErrors,
bool suppressErrors, bool& isOverriden,
bool& anyErrors) const {
auto reportError = [&](auto& param) {
anyErrors = true;
Expand All @@ -131,8 +131,10 @@ const ParameterSymbolBase& ParameterBuilder::createParam(const Definition::Param

auto& comp = scope.getCompilation();
const ExpressionSyntax* newInitializer = nullptr;
if (auto it = assignments.find(decl.name); it != assignments.end())
if (auto it = assignments.find(decl.name); it != assignments.end()) {
newInitializer = it->second;
isOverriden = newInitializer != nullptr;
}

if (decl.isTypeParam) {
auto param = comp.emplace<TypeParameterSymbol>(decl.name, decl.location, decl.isLocalParam,
Expand Down
3 changes: 2 additions & 1 deletion source/symbols/ParameterBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class ParameterBuilder {

const ParameterSymbolBase& createParam(const Definition::ParameterDecl& decl,
SourceLocation instanceLoc, bool forceInvalidValues,
bool suppressErrors, bool& anyErrors) const;
bool suppressErrors, bool& isOverriden,
bool& anyErrors) const;

static void createDecls(const Scope& scope, const ParameterDeclarationBaseSyntax& syntax,
bool isLocal, bool isPort, SmallVector<Decl>& results);
Expand Down
15 changes: 15 additions & 0 deletions tests/unittests/ClassTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2502,3 +2502,18 @@ endclass
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
}

TEST_CASE("Class parameter defaults regression") {
auto tree = SyntaxTree::fromText(R"(
class C #(type T1=int, T2=T1, parameter T1 foo = 1, parameter int bar = foo);
endclass
module m;
C c = new;
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
}

0 comments on commit f90e406

Please sign in to comment.