Spotted from #2760
Public Sub Temp()
Const foo = 12, bar = 42 ' bar is not used
MsgBox foo
End Sub
Const foo = 12, bar = 42 ' bar is not used has foo and bar both as an Integer in the Locals window. Inspection results have them coming up as ... implicitly Variant.
IOW a Const can't be an implicit Variant, and we need the resolver to infer the correct type from the assigned value.
Infering the type could get rather complicated. In particular, it can only happen in the reference resolver. The problem is that the constant expression on the right-hand-side can contain references to other constants, possibly in different modules and possibly themselves not explicitly typed. Moreover, we will have to be able to determine the type after all implicit type conversions that might happen, e.g. due to arithmetic operators.
Basically, we will need a new compilation pass that determines the type of so far untyped constants. Moreover, we should save the information that they have been untyped so that we can show them as the results of a new NotExplicitlyTypedConstant inspection.
That last one should be pretty straightforward, and we can implement a simplified version even without fixing the resolver first, since the implicit typing can be seen in the parse-tree. The Inspection could assume that all names are explicitly typed and only call out implicitly typed literals.
This can be a difficulty-01/duckling level issue, if we postpone the resolver fix and instead simply make the implicit variant inspection ignore Const declarations.
For reference, from the VBA spec:
If an <untyped-name-const-item> does not include a <const-as-clause>, the declared type of
the constant is the same as the declared type of its <constant-expression> element. Otherwise,
the constant鈥檚 declared type is the declared type of the <BUILTIN-TYPE> element of the <const-asclause>.
Note this this also needs to be aware of type hints:
Public Sub Temp()
Const foo = 12&
Const bar = 12
Const baz& = 12 '<--No inspection result.
MsgBox TypeName(foo) 'Long
MsgBox TypeName(bar) 'Integer
MsgBox TypeName(baz) 'Long
End Sub
FWIW the type hinted constant (<typed-name-const-item>) baz& works.
Most helpful comment
This can be a difficulty-01/duckling level issue, if we postpone the resolver fix and instead simply make the implicit variant inspection ignore
Constdeclarations.