diff --git a/source/ast/Lookup.cpp b/source/ast/Lookup.cpp index 87dc071d3..20a7491a0 100644 --- a/source/ast/Lookup.cpp +++ b/source/ast/Lookup.cpp @@ -1621,10 +1621,16 @@ bool Lookup::withinClassRandomize(const ASTContext& context, const NameSyntax& s if (!isClass.has_value() || !isClass.value()) return false; - return resolveColonNames(nameParts, colonParts, name, flags, result, classContext); + if (!resolveColonNames(nameParts, colonParts, name, flags, result, classContext)) + return false; + } + else { + if (!lookupDownward(nameParts, name, classContext, flags, result)) + return false; } - return lookupDownward(nameParts, name, classContext, flags, result); + unwrapResult(*context.scope, syntax.sourceRange(), result); + return true; } bool Lookup::findAssertionLocalVar(const ASTContext& context, const NameSyntax& syntax, diff --git a/tests/unittests/ast/ClassTests.cpp b/tests/unittests/ast/ClassTests.cpp index 7d6ffb06f..a9198e447 100644 --- a/tests/unittests/ast/ClassTests.cpp +++ b/tests/unittests/ast/ClassTests.cpp @@ -3483,3 +3483,30 @@ endmodule compilation.addSyntaxTree(tree); NO_COMPILATION_ERRORS; } + +TEST_CASE("Class randomize can't access protected members") { + auto tree = SyntaxTree::fromText(R"( +class C; + protected int a; + rand bit b; +endclass + +class T; + function f(); + C c = new(); + int i = c.randomize() with { + if (a == 3) { + b == 1'b1; + } + }; + endfunction +endclass +)"); + + Compilation compilation; + compilation.addSyntaxTree(tree); + + auto& diags = compilation.getAllDiagnostics(); + REQUIRE(diags.size() == 1); + CHECK(diags[0].code == diag::ProtectedMemberAccess); +}