Skip to content

Commit 0cac5e6

Browse files
authored
Merge pull request #5863 from MDoerner/CorrectlyResolveReferencesToDeclarationNamedLikeBuiltInTypes
Correctly resolve references to declarations named like built-in types
2 parents 2039bbf + 0ef8b96 commit 0cac5e6

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Rubberduck.Parsing/Binding/DefaultBindingContext.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private IExpressionBinding Visit(Declaration module, Declaration parent, VBAPars
157157
case VBAParser.MarkedFileNumberExprContext markedFileNumberExprContext:
158158
return Visit(module, parent, markedFileNumberExprContext, withBlockVariable);
159159
case VBAParser.BuiltInTypeExprContext builtInTypeExprContext:
160-
return Visit(builtInTypeExprContext);
160+
return Visit(module, parent, builtInTypeExprContext, statementContext);
161161
//We do not handle the VBAParser.TypeofexprContext because that should only ever appear as a child of an IS relational operator expression and is specifically handled there.
162162
default:
163163
throw new NotSupportedException($"Unexpected expression type {expression.GetType()}");
@@ -209,8 +209,17 @@ private IExpressionBinding Visit(Declaration module, Declaration parent, VBAPars
209209
return Visit(module, parent, expression.expression(), withBlockVariable, StatementResolutionContext.Undefined);
210210
}
211211

212-
private IExpressionBinding Visit(VBAParser.BuiltInTypeExprContext expression)
212+
private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.BuiltInTypeExprContext expression, StatementResolutionContext statementContext)
213213
{
214+
//It is legal to name variables like built-in types.
215+
//So, we try and see whether it resolves.
216+
var builtInTypeNamedNameBinding = new SimpleNameDefaultBinding(_declarationFinder, Declaration.GetProjectParent(parent), module, parent, expression, Identifier.GetName(expression.builtInType()), statementContext);
217+
var resolutionResult = builtInTypeNamedNameBinding.Resolve();
218+
if (resolutionResult.Classification != ExpressionClassification.ResolutionFailed)
219+
{
220+
return builtInTypeNamedNameBinding;
221+
}
222+
214223
// Not actually an expression, but treated as one to allow for a faster parser.
215224
return new BuiltInTypeDefaultBinding(expression);
216225
}

Rubberduck.Parsing/Symbols/Identifier.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ public static string GetName(VBAParser.SimpleNameExprContext context)
190190
return GetName(context.identifier());
191191
}
192192

193+
public static string GetName(VBAParser.BuiltInTypeContext context)
194+
{
195+
return context.GetText();
196+
}
197+
193198
public static string GetName(VBAParser.IdentifierContext context, out Interval tokenInterval)
194199
{
195200
tokenInterval = Interval.Of(context.Start.TokenIndex, context.Stop.TokenIndex);

RubberduckTests/Inspections/ParameterNotUsedInspectionTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public void ParameterUsed_DoesNotReturnResult()
4848
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
4949
}
5050

51+
[Test]
52+
[Category("Inspections")]
53+
//See issue #5336 at https://github.com/rubberduck-vba/Rubberduck/issues/5336
54+
public void ParameterWithBuiltInTypeNameUsed_DoesNotReturnResult()
55+
{
56+
const string inputCode =
57+
@"Public Function Foo(Object As Object) As Object
58+
Set Foo = Object
59+
End Function";
60+
61+
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
62+
}
63+
5164
[Test]
5265
[Category("Inspections")]
5366
public void ParameterNotUsed_ReturnsResult_SomeParamsUsed()

0 commit comments

Comments
 (0)