Skip to content

Compiler does not infer string type for nameof pattern #13377

Closed
@niesmo

Description

@niesmo

When using nameof in a pattern match, I'd expect the compiler to infer the argument type as string since nameof returns a string. However, the argument is treated as generic and generates invalid IL.

Repro steps

// Expected:
//   FS0001: This expression was expected to have type 'int' but here has type 'string'
// Actual:
//   No compile error; InvalidProgramException at runtime.
match 3 with
| nameof int -> true
| _ -> false
// Expected inferred type:
//   val f : x:string -> bool
// Actual inferred type:
//   val f : x:'a -> bool
let f x = match x with nameof x -> true | _ -> false

According to the RFC for the nameof pattern feature, the nameof expr should be "equivalent to a literal string pattern" which is not being respected.

Expected behavior

Expect the compiler to infer argument type as string when nameof is used in a pattern match.

Actual behavior

Compiler infers argument type as generic argument when nameof is used in a pattern match.

Known workarounds

Explicit type annotation.

let f (x:string) = match x with nameof x -> true | _ -> false

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBugImpact-High(Internal MS Team use only) Describes an issue with extreme impact on existing code.good first issuehelp wanted

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions