Skip to content

Later errors obscure earlier errors  #13697

@abelbraaksma

Description

@abelbraaksma

Using typof<SomeType> can lead to confusing error messages when used as an argument for an attribute.

Repro steps

Try this code:

[<AttributeUsage(AttributeTargets.All)>]
type FooAttribute(t:System.Type) =
    inherit System.Attribute()

[<Foo(typeof<OutOfScopeType>)>]
type Vehicle() = class end

Expected behavior

Should give the same error as normal use of typeof, i.e. that the type is not defined. The expected error is: "FS0039 The type 'OutOfScopeType' is not defined."

In other words, I'd expect it similar to this:

image

Actual behavior

A different error is shown and the whole expression is underlined.

FS0267 This is not a valid constant expression or custom attribute value

image

I should note that if you hover over the type-part, both errors are shown.

Known workarounds

N/A

Related information

This reported has lead to confusion with users. For instance, see this post, where a programmer thought that typeof as a whole was not allowed (and I know I fell for this trap in the past as well): https://stackoverflow.com/questions/18369922/f-attributes-typeof-and-this-is-not-a-constant-expression.

Metadata

Metadata

Assignees

Labels

AI-thinks-issue-fixedArea-Diagnosticsmistakes and possible improvements to diagnosticsBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.Theme-Simple-F#A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.

Type

Projects

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions