diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2020-02-06 08:38:46 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-06 08:38:46 -0800 |
| commit | 9c84cceffba26817721a23a1a85a48644bf3a560 (patch) | |
| tree | 067f6fcdfa496adf5037ae771ff52ad50ce1e289 /source/slang/slang-check-expr.cpp | |
| parent | b42b865ac7ce144561fa17743616e550eeae7102 (diff) | |
Improve checks and diagnostics around redeclarations (#1201)
* Improve checks and diagnostics around redeclarations
This change turns checking for redeclarations into a dedicated phase of semantic checking, and ensures that it applies to the main categories of declarations: functions, types, and variables.
Note that "variables" here includes function parameters and `struct` fields in addition to the more obvious global and local variables.
Some of the logic for checking redeclarations already existed for functions, and was refactored to deal with other cases of declarations. The checking for functions still needs to be special-cased because functions are much more flexible about the kinds of redeclarations that are allowed.
In addition to improving the diagnosis of redeclaration itself, this change also changes the error message that is produced when referencing a symbol that is ambiguous due to begin redeclared.
This is a small quality-of-life fix, and has the benefit of being much easier to implement than robust tracking of what variables have had redeclaration errors issued so that we can skip emitting an ambiguity error at the use site.
A new test case was added to cover the redeclaration cases for variables (but not types or functions), and the test for function parameters was updated to account for the new more universal diagnostic message (since function parameters used to have special-case redeclaration checking).
* fixup: missing file
Diffstat (limited to 'source/slang/slang-check-expr.cpp')
| -rw-r--r-- | source/slang/slang-check-expr.cpp | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index e7db665ae..ebc3a3ef9 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -435,6 +435,29 @@ namespace Slang return result; } + void SemanticsVisitor::diagnoseAmbiguousReference(OverloadedExpr* overloadedExpr, LookupResult const& lookupResult) + { + getSink()->diagnose(overloadedExpr, Diagnostics::ambiguousReference, lookupResult.items[0].declRef.GetName()); + + for(auto item : lookupResult.items) + { + String declString = getDeclSignatureString(item); + getSink()->diagnose(item.declRef, Diagnostics::overloadCandidate, declString); + } + } + + void SemanticsVisitor::diagnoseAmbiguousReference(Expr* expr) + { + if( auto overloadedExpr = as<OverloadedExpr>(expr) ) + { + diagnoseAmbiguousReference(overloadedExpr, overloadedExpr->lookupResult2); + } + else + { + getSink()->diagnose(expr, Diagnostics::ambiguousExpression); + } + } + RefPtr<Expr> SemanticsVisitor::_resolveOverloadedExprImpl(RefPtr<OverloadedExpr> overloadedExpr, LookupMask mask, DiagnosticSink* diagSink) { auto lookupResult = overloadedExpr->lookupResult2; @@ -474,13 +497,7 @@ namespace Slang // if( diagSink ) { - getSink()->diagnose(overloadedExpr, Diagnostics::ambiguousReference, lookupResult.items[0].declRef.GetName()); - - for(auto item : lookupResult.items) - { - String declString = getDeclSignatureString(item); - getSink()->diagnose(item.declRef, Diagnostics::overloadCandidate, declString); - } + diagnoseAmbiguousReference(overloadedExpr, lookupResult); // TODO(tfoley): should we construct a new ErrorExpr here? return CreateErrorExpr(overloadedExpr); |
