question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type inference may cause BC30455 when inferred-type variable name matches module method name

See original GitHub issue

Version Used:

Microsoft Visual Studio Enterprise 2019 Preview
Version 16.2.0 Preview 2.0
VisualStudio.16.Preview/16.2.0-pre.2.0+29006.145
Microsoft .NET Framework
Version 4.8.03752

[..]

Visual Basic Tools   3.2.0-beta2-19303-01+c9689b7ad571762df662bec55472938d18705dca

Steps to Reproduce:

  1. Create a WPF app project.
  2. Open MainWindow.xaml.vb, and replace its contents with the following:
Option Strict On
Option Infer On

Class MainWindow
    Sub Foo()
        Dim dataObject As DataObject
        For Each format In dataObject.GetFormats()

        Next
    End Sub
End Class

Expected Behavior: This code should compile as is. format should be of type System.String.

Actual Behavior: The compiler believes format to be an invocation of the method Microsoft.VisualBasic.Strings.Format().

It gives the error:

Error | BC30455 | Argument not specified for parameter ‘Expression’ of ‘Public Function Format(Expression As Object, [Style As String = “”]) As String’.

It also renames format to Format (note casing) for the same reason.

Workarounds:

  • A different variable name works fine. For example, given the following, the compiler correctly recognizes the type, doesn’t rename the variable, and doesn’t show an error:
Dim dataObject As DataObject
For Each formatName In dataObject.GetFormats()

Next
  • Likewise, an explicit type avoids this issue:
For Each format As String In dataObject.GetFormats()

Next

Additional notes:

In an existing project, this error only started occurring after I migrated the project to the Sdk style. The full runtime is used (<VBRuntime>default</VBRuntime>).

However, in a fresh project, the error appears immediately, suggesting this may be a toolchain regression.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sfoslundcommented, Oct 25, 2019

PR #3723 confirmed that this is solved by disabling implicit imports with the DisableImplicitNamespaceImports

0reactions
dsplaistedcommented, Sep 13, 2019

It looks like the implicit imports can be disabled with the DisableImplicitNamespaceImports property.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The type for variable '<variablename>' will not be inferred ...
The type for variable '<variablename>' will not be inferred because it is bound to a field in an enclosing scope. Either change the...
Read more >
Documentation - Type Inference
This kind of inference takes place when initializing variables and members, setting parameter default values, and determining function return types. In most ...
Read more >
Why can't C# infer type from this seemingly simple, obvious ...
But still, a method name is NOT an explicit Action<> Object. And therefor it won't infer the type as an Action type.
Read more >
Type inference - Kotlin language specification
Kotlin has a concept of type inference for compile-time type information, meaning some type information in the code may be omitted, to be...
Read more >
Why Infer Types?
By requiring parameter and return types to be declared, type inference will always report errors in the correct function.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found