2.6.2 Semantic Compilation Errors
A semantic compilation error is raised by the static semantics layer while walking the abstract syntax tree (AST) with symbol information. Examples are a duplicate declaration, an undefined name, and a type mismatch in a constant expression.
| Code family | VBC, range VBC09300–VBC09999 |
| Title | Compile error |
| Raised by | the static semantics layer (abstract syntax tree) |
| Source metadata | VBCompileErrorInfo |
| Severity | Error |
| Detail | the offending symbol / expression, on Diagnostic.data |
Numeric Range
VBCompileErrorId reserves the range [9300..] for semantic compilation errors: errors issued from the static semantics analysis pass report a value greater than or equal to 9300. MS-VBAL does not distinguish a compile-time error raised in CST semantics from one raised in AST semantics; RDCore splits syntax errors and semantic compilation errors by numeric range only.
Compilation errors issued from a language core extension report a VBCompileErrorId value between 8000 and 9299; see RD-VBAL §1.1.4 Core Diagnostics.
Emission
Semantic compilation errors are emitted by the resolver and the static semantic pass.
ICoreDiagnosticsFactory.FromVBCompileError is the projection for semantic compilation errors, the counterpart of FromVBSyntaxError, and SemanticContextBuilder.AddDiagnosticOnError uses it. No production code path calls AddDiagnosticOnError, so semantic compilation errors are not currently projected to LSP diagnostics.
Note
Not implemented. The RDCore.Diagnostics handler for rdcore/diagnostics/document projects the syntax errors of the parse result only. Semantic compilation errors do not reach the textDocument/diagnostic pull through it; see RD-VBAL §2.6.5 Diagnostics Pipeline.
Published Codes
| Code | Title | Condition |
|---|---|---|
VBC09306 |
Invalid ReDim | a ReDim re-dimensions a member of an object declared As Object (of a declared class, it is VBC09316) |
VBC09307 |
Expected array | LBound or UBound of what never holds an array: a variable declared as a number, a String, a Boolean, a Date, an object, a class or a user-defined type; a project, a module or an enumeration |
VBC09309 |
Label not defined | a jump names a line label or line number the procedure does not define |
VBC09310 |
Type mismatch | a value is of a type that cannot be let-coerced to the type it is used as |
VBC09312 |
Exit Do not within Do...Loop | an Exit Do statement is not lexically inside a Do loop |
VBC09313 |
Exit For not within For...Next | an Exit For statement is not lexically inside a For or For Each loop |
VBC09314 |
Exit Function not allowed in Sub or Property | an Exit Function statement is in a Sub, or in a Property Let or Property Set (it is accepted in a Property Get, as MS-VBA does) |
VBC09315 |
Exit Property not allowed in Sub or Function | an Exit Property statement is in a Sub or a Function |
VBC09316 |
Method or data member not found | a member is accessed on a type known not to have it |
VBC09317 |
Invalid use of Me | Me is written where there is no object it could be |
VBC09318 |
With expression outside With block | a leading-dot member has no enclosing With block |
VBC09319 |
Duplicate label definition | a procedure defines the same line label or line number more than once |
VBC09320 |
Inconsistent property accessors | a property's Get/Let/Set sharing a name do not together describe one valid property |
VBC09321 |
Argument required for Property Let or Property Set | a Property Let or Property Set declares no parameters at all |
VBC09322 |
Event not defined | a RaiseEvent names an event the class module it is written in does not declare |
VBC09323 |
Wrong number of arguments or invalid argument for event | a RaiseEvent's arguments are not compatible with the parameter list of its event |
VBC09324 |
Invalid type for WithEvents variable | a WithEvents variable is not declared as a specific class that has an event, or is declared as the class of its own module |
VBC09325 |
Invalid event name | the name of an Event declaration contains an underscore |
VBC09326 |
Invalid event handler | a procedure named for a WithEvents variable and an event of its class is not a subroutine, or has a parameter list incompatible with the event's |
VBC09327 |
ByVal argument not allowed here | no longer raised: an argument written ByVal where MS-VBA refuses it is the error MS-VBA reports (RD-VBAL §5.6.13.1) |
VBC09328 |
Invalid Implements directive | an Implements directive names the class itself, a class another directive names, a class with an underscore in a public member, or an interface whose prefix begins another's |
VBC09329 |
Object module needs to implement all members of its interface | a public variable or method of an interface class has no implemented name declaration |
VBC09330 |
Invalid implemented member | an implemented name declaration is not the kind of declaration its member is, or its parameters or type are not equivalent |
VBC09331 |
Sub or Function not defined | a statement the language the code is written in does not have: a bare Print outside the platform's BASIC |
VBC09332 |
Exit Sub not allowed in Function or Property | an Exit Sub statement is in a Function or a property |
VBC09333 |
Variable required | an expression that is certainly not a variable (a literal, an operator's result, a constant, a function of the library) is the target of an assignment (Let, Set, LSet, RSet; but a Let may assign to a function of the library whose declared type is a class, Object or Variant), of a Mid statement or of a ReDim statement, the control variable of a For or For Each loop, or the variable a Line Input #, Input # or Get reads into; or a constant is an element of an Erase statement |
VBC09334 |
Access not valid for the file mode | the Access clause of an Open statement is not one its For mode allows |
VBC09335 |
Type-declaration character does not match declared data type | a name is written with the type-declaration character of a type other than the one it was declared as |
VBC09336 |
Private Enum and user defined types cannot be used as parameters or return types for public procedures, public data members, or fields of public user defined types | a public member is declared with a private Enum - a variable, a result, a parameter - or, in a class module, with a private user-defined type; as MS-VBA has it, not a field of a Public Type, and not a private type in a standard module |
VBC09337 |
Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules | a class module declares one of them as a public member, or a Global variable |
VBC09338 |
Cannot define a Public user-defined type within an object module | a class module declares a Public Type, or a Type with no modifier |
VBC09339 |
Argument not optional | an object whose default member takes an argument (a Collection) is where a value is expected: assigned to a variable of a value type, an operand, an argument of a parameter of a value type or of a library function that reads a value; see RD-VBAL §5.5.1.1 |
VBC09340 |
Expected Function or variable | a Sub where a value is expected |
VBC09341 |
Can't assign to read-only property | a Let of a property that has a Property Get and no Property Let |
VBC09342 |
Wrong number of arguments or invalid property assignment | an argument no parameter takes |
VBC09343 |
LSet allowed only on strings and user-defined types | the target of an LSet statement |
VBC09344 |
RSet allowed only on strings | the target of an RSet statement |
VBC09345 |
Invalid use of property | a Set of a property that has a Property Get and no Property Set, and of the default instance of an extensible class (a document module) |
VBC09346 |
Assignment to constant not permitted | a Let or a Set of a constant |
VBC09347 |
Object required | a Set of a variable that holds no object |
VBC09348 |
Named argument already specified | a parameter given two arguments |
VBC09349 |
Named argument not found | a named argument the procedure does not declare |
VBC09350 |
Constant expression required | a constant's value reads a variable or calls a function, or names the constant itself |
VBC09351 |
Invalid data type for constant | a constant's value is Nothing |
VBC09352 |
Overflow | a constant's value does not fit its type |
VBC09353 |
Division by zero | a constant's value divides by zero |
VBC09354 |
Invalid qualifier | a member is accessed on a value that has no members |
VBC09355 |
Function call on left-hand side of assignment must return Variant or Object | a call of a function is assigned |
VBC09356 |
Wrong number of dimensions | an element of an array is selected by the wrong number of subscripts |
VBC09357 |
Array already dimensioned | an array declared with fixed dimensions is given others |
VBC09358 |
For control variable already in use | a loop is controlled by the variable of a loop it is nested in |
VBC09359 |
Invalid Next control variable reference | a Next statement names a variable that is not the control variable of its loop |
VBC09360 |
Can't assign to array | an array declared with fixed dimensions is assigned an array |
VBC09361 |
ByRef argument type mismatch | a variable is passed to a ByRef parameter of another type |
VBC09362 |
Type mismatch: array or user-defined type expected | an array is passed to a parameter declared as an array of another type |
VBC09363 |
Expected variable or procedure, not module | the name of a standard module is used by itself, as a value or a procedure |
VBC09364 |
Expected Sub, Function, or Property | a call statement names a variable or a constant of the procedure, not a procedure |
VBC09365 |
Range has no values | an array is declared with an upper bound less than its lower bound |
VBC09366 |
A module is not a valid type | a declaration is typed with the name of a standard module |
VBC09367 |
Invalid use of AddressOf operator | an AddressOf expression names a method of a class module, or of an object |
VBC09368 |
Invalid use of New keyword | a variable is declared As New of a type that is no class, of a library's class that cannot be created, or WithEvents |
VBC09369 |
Can't change data types of array elements | a ReDim gives an array elements of another type than it is declared with, As New included |
VBC09370 |
Expected procedure, not variable | a call statement names a variable or a constant of a module, not a procedure |
VBC09371 |
Array argument must be ByRef | an argument of an array parameter is written ByVal |
VBC09372 |
Member already exists in an object module from which this object module derives | the code of a form declares something with the name of one of its controls |
VBC09373 |
Automation type not supported in Visual Basic | a type is written with the name of a library's module, Strings |
VBC09374 |
Expected user-defined type, not project | a type is written with the name of a project or a library, VBA |
VBC09375 |
Invalid length for fixed-length string | a String * n is declared with a length under 1 or past 65526 |
VBC09376 |
Expected variable or procedure, not project | a project, VBA, is written where a value is |
VBC09377 |
Expected variable or procedure, not enum type | an enumeration, Color, is written where a value is |
VBC09378 |
With object must be user-defined type, Object, or Variant | a With block is about a value that has no members |
VBC09379 |
For Each may only iterate over a collection object or an array | a For Each loop iterates a value that has no elements |
Instruction-list lowering reports VBC09309 for a Jump/JumpTable label operand that does not resolve, and VBC09319 for a repeated label definition; see RD-VBAL §3.5.3 Lowering Block Statements. VBC09320 and VBC09321 are raised by ScopeTreeSymbolResolver; see RD-VBAL §2.3.1.3 Name Resolution. VBC09322 and VBC09323 are raised by StatementStaticSemanticsEvaluator; see RD-VBAL §5.4.2.20 RaiseEvent Statement. VBC09324, VBC09325 and VBC09326, and a repeated event name as VBC09303, are raised by ClassModuleEventSemantics, which checks what a class module declares about events; see RD-VBAL §5.2.4.3, §5.2.3.1.2 and §5.3.1.8. An argument written ByVal where MS-VBA refuses it (VBC09310, VBC09371, or VBC00001) is reported by ByValArgumentStaticSemantics; see RD-VBAL §5.6.13.1 Argument Lists. VBC09328, VBC09329 and VBC09330, and VBC09311 for an interface class that does not exist, are raised by ImplementsSemantics; see RD-VBAL §5.2.4.2 and §5.3.1.9.
Other Compilation Errors
This specification refers to the following semantic compilation errors on other pages.
| Code | VBCompileErrorId |
Condition | See |
|---|---|---|---|
VBC09301 |
AmbiguousName |
Within one tier of the project or global scope, a name resolves to more than one symbol of the highest rank: members of two modules of the workspace (of a class module, only a public Enum or user-defined type and that Enum's members), or of two modules of one library, the VBA library included. The symbols of two libraries never share a rank. The reference must qualify the name. A declared type whose name ties this way is VBC09311 instead. |
RD-VBAL §2.3.1.3 Name Resolution |
VBC09302 |
VariableNotDefined |
An unresolved simple name. The static-semantics layer consumes the module's ModuleDirectives to decide whether such a name is a deferred VBUnknownType or this error. |
RD-VBAL §2.3.1.3 Name Resolution |
VBC09303 |
DuplicateDeclaration |
A name is declared more than once within one module or procedure. | RD-VBAL §2.3.1.3 Name Resolution |
VBC09304 |
InvalidUseOfObject |
The Nothing literal where a value is expected (n = Nothing), and a conditional-compilation expression that is an object. |
RD-VBAL §5.5.1.1 Static Semantics |
VBC09305 |
InvalidParamArrayUse |
A ParamArray parameter, bare or parenthesized, is the argument of a ByRef parameter of a procedure of the workspace's own. |
RD-VBAL §5.3.1.11 Procedure Invocation Argument Processing |
VBC09311 |
UserDefinedTypeNotDefined |
An unknown type should raise this error, and so does a TypeOf ... Is whose type is a name that binds no type (a variable's, a procedure's); a module or a project is VBC09366, VBC09373 or VBC09374 instead. Verbose diagnostic messages should help clarify its meaning. |
RD-VBAL §2.4.2 Non-intrinsic Types, RD-VBAL §5.6.16.7 Type Expressions |
An appropriate compile-time error should be issued for a duplicate declaration and for an ambiguous name. The symbol resolver reports the error kind; the caller, which knows where the reference is, builds the located diagnostic.
⏮️ RD-VBAL §2.6.1 Syntax Errors | ⏭️ RD-VBAL §2.6.3 Runtime Errors