Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetAssignmentWithIncompatibleObjectTypeInspection #5003

Merged

Conversation

MDoerner
Copy link
Contributor

@MDoerner MDoerner commented Jun 10, 2019

Closes #4981
Closes #5069
Closes #5071
Closes #3937

This PR already covers the scope layed out in the first issue above, i.e. it curently supports variable assignments on the RHS. However, I think that this is not too useful yet. So, I plan to extend the scope to new expressions, index expressions and member access expressions on the RHS.

Currently only supports variable assignments on the RHS.

Also fixes a previously ignored test for the MoveCloserToUsageRefactoring.
# Conflicts:
#	Rubberduck.CodeAnalysis/Rubberduck.CodeAnalysis.xml
@MDoerner MDoerner added the PR-Status: WIP Pull request is work-in-progress, more commits should be expected. label Jun 10, 2019
…ypeInspection into ISetTypeResolver implementation

Currently only handles simple name expressions and instance expressions.

Also fixed Declaration.FullAsTypeName for enums and UDTs.
# Conflicts:
#	Rubberduck.Resources/Inspections/InspectionInfo.Designer.cs
#	Rubberduck.Resources/Inspections/InspectionInfo.de.resx
#	Rubberduck.Resources/Inspections/InspectionNames.Designer.cs
#	Rubberduck.Resources/Inspections/InspectionNames.de.resx
#	Rubberduck.Resources/Inspections/InspectionResults.Designer.cs
#	Rubberduck.Resources/Inspections/InspectionResults.de.resx
…-object expressions

null is only returned in case we cannot resolve the Set type, primarily because of member accesses on objects of type Variant and Object.
This includes a workaround requires because built-in types are currently resolved as lExpressions, more precisely as simpleNameExpressions.
Previously, everything matched by builtInTypeExpr was already consumed by lExpr. Since all built-in types are keywords, this should not cause problems in most cases. The only exception would be foreign identifiers whose name is a built-in type. (This should be very rare and is problematic anyway.)

This change required patching up the resolver as well to deal with builtInTypes.
…th compatible parameter list with only optional arguments
# Conflicts:
#	Rubberduck.Resources/Inspections/InspectionInfo.Designer.cs
#	Rubberduck.Resources/Inspections/InspectionInfo.resx
#	Rubberduck.Resources/Inspections/InspectionNames.Designer.cs
#	Rubberduck.Resources/Inspections/InspectionNames.resx
#	Rubberduck.Resources/Inspections/InspectionResults.Designer.cs
#	Rubberduck.Resources/Inspections/InspectionResults.resx
Also cleans up NonReturningFunctionInspection and makes Selection honor the contract of IComparable<Selection>.
@MDoerner MDoerner added PR-Status: Review Requested No more commits, PR is ready for the eyes that need to see it. and removed PR-Status: WIP Pull request is work-in-progress, more commits should be expected. labels Aug 10, 2019
@MDoerner MDoerner marked this pull request as ready for review August 10, 2019 12:26
@MDoerner
Copy link
Contributor Author

MDoerner commented Aug 10, 2019

This PR is largely finished now. It introduces a SetTypeResolver able to provide the set type name and declaration for all expressions for which these can be reolved at compile time. This is used to supply the set type name for the inspection in order to compare it with the LHS of the Set assignment.

The current implementation uses identifier references to determine the Set type. This has the advandage over putting the information on the contextx themselves that no further cash invalidation is required.

I still plan to do some clean up in IndexDefaultBinding and BoundExpressionVisitor around the resolution of index expressions. However, I would already apprechieate any feedback I could get, especially on the Set resolver.

These reference the entire index expression and have the new flag IsArrayAccess set to true. To resolve everything correctly, the entire IndexDefaultBinding got an overhaul.

Also replaces the tests of ImplicitDefaultMemberAssignmentInspection since they previously only passed because the resolution of the assignment target was wrong.
…to the inspection itself

This also makes the ImplicitDefaultMemberAssignmentInspection work correctly without tweaks to the resolver..
Previously the options without explicit parentheses were preferred and matched to expressions with parentheses consuming them as part or the argument.
@MDoerner
Copy link
Contributor Author

I basically rewrote the resolution of index expressions in the last commits to simplify things in the resolver. This includes attaching references to array accesses with a new flag IsArrayAccess. I also added the new flag IsDefaultMemberAccess to better identify these accesses. These are now also part of the IndexExpression, which was the basic change alllowing for a cleaner handling of these situations.

I do not plan to add anything further to this PR excpt in response to review comments.

Copy link
Contributor

@bclothier bclothier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent job, MDoerner! Mainly nitpicks. I also have one general comment - there are a numbers of TODOs but the comment there does not really explain what exactly needs to be done. For example, instead of Need to improve checks, it should explain what is missing so that maintainers who happen to go through will understand what needs doing there.

[Test]
[Ignore("Broken by COM collector fix. See comment on test.")]
//[Ignore("Broken by COM collector fix. See comment on test.")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's working, do we really need the comment (both of them) anymore?

Copy link
Contributor Author

@MDoerner MDoerner Aug 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed to remove the ignore line entirely. Will do that later.

Copy link
Member

@retailcoder retailcoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clean code as always - only a few minor suggestions, other than that LGTM.

/// <why>
/// The VBA compiler does not check whether different object types are compatible. Instead there is a runtime error whenever the types are incompatible.
/// </why>
/// <example>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <example>
/// <example hasResult="true">

/// End Sub
/// ]]>
/// </example>
/// <example>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <example>
/// <example hasResult="false">

*/

return ResolveRecursiveDefaultMember(defaultMember, defaultMemberClassification, argumentList, expression, recursionDepth);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job with the comments there 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are basically copied from IndexDefaultBinding, where autoboosh added them. I just modified them to refer to a dictionary access expression instead of an index expression.

@@ -60,6 +64,10 @@ public class IdentifierReference : IEquatable<IdentifierReference>

public bool IsSetAssignment { get; }

public bool IsDefaultMemberAccess { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant 👍

//Please note that the lExpression is the (whitespace) index expression itself and not the lExpression it contains.
private Declaration ResolveIndexExpressionAsArrayAccess(VBAParser.LExpressionContext actualIndexExpr, QualifiedModuleName containingModule, DeclarationFinder finder)
{
// A n array access references the entire (whitespace)indexExpr.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// A n array access references the entire (whitespace)indexExpr.
// An array access references the entire (whitespace)indexExpr.

@@ -386,6 +386,9 @@
<data name="ObsoleteWhileWendStatementInspection" xml:space="preserve">
<value>Use of obsolete 'While...Wend' statement</value>
</data>
<data name="SetAssignmentWithIncompatibleObjectTypeInspection" xml:space="preserve">
<value>Set Assignment With Incompatible Object Type</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<value>Set Assignment With Incompatible Object Type</value>
<value>Set assignment with incompatible object type</value>

Attribute Foo.VB_UserMemId = 0
End Property
Public Property Let Something(ByVal value As Long)
Attribute Foo.VB_UserMemId = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it makes a difference, but is this legal code? IIUC only one member is allowed to have VB_UserMemId = 0... I've always only ever put it on the Property Get member.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea. I guess the VBE would just ignore one of them.

…tTypeInspection

Also changes the way the set assignments are obtained to increase performance when libraries are referenced.
@bclothier bclothier merged commit 10a0572 into rubberduck-vba:next Aug 15, 2019
@MDoerner MDoerner removed the PR-Status: Review Requested No more commits, PR is ready for the eyes that need to see it. label Aug 29, 2019
@MDoerner MDoerner deleted the IncompatibleObjectTypeInspection branch February 19, 2020 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants