Skip to content

Commit a7a66e3

Browse files
authored
Merge pull request #7125 from KevinRansom/langversion-nameof
Langversion for nameof feature
2 parents b26941b + 26342bc commit a7a66e3

15 files changed

Lines changed: 816 additions & 321 deletions

File tree

FSharpTests.Directory.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
DisabledWarnings="$(NoWarn)"
1414
DotnetFsiCompilerPath="$(DotnetFsiCompilerPath)"
1515
FsiExec="@(FsiExec)"
16+
LangVersion="$(LangVersion)"
1617
LCID="$(LCID)"
1718
LoadSources="@(LoadSource)"
1819
NoFramework="false"

src/fsharp/CompileOps.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4730,7 +4730,7 @@ and [<Sealed>] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
47304730
let tcGlobals = TcGlobals(tcConfig.compilingFslib, ilGlobals, fslibCcu,
47314731
tcConfig.implicitIncludeDir, tcConfig.mlCompatibility,
47324732
tcConfig.isInteractive, tryFindSysTypeCcu, tcConfig.emitDebugInfoInQuotations,
4733-
tcConfig.noDebugData, tcConfig.pathMap)
4733+
tcConfig.noDebugData, tcConfig.pathMap, tcConfig.langVersion)
47344734

47354735
#if DEBUG
47364736
// the global_g reference cell is used only for debug printing

src/fsharp/FSharp.Build/Fsi.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type public Fsi () as this =
2929
let mutable disabledWarnings : string = null
3030
let mutable dotnetFsiCompilerPath : string = null
3131
let mutable fsiExec = false
32+
let mutable langVersion : string = null
3233
let mutable noFramework = false
3334
let mutable optimize = true
3435
let mutable otherFlags : string = null
@@ -67,6 +68,7 @@ type public Fsi () as this =
6768

6869
builder.AppendSwitchIfNotNull("--codepage:", codePage)
6970

71+
builder.AppendSwitchIfNotNull("--langversion:", langVersion)
7072
if noFramework then builder.AppendSwitch("--noframework")
7173

7274
if defineConstants <> null then
@@ -167,6 +169,10 @@ type public Fsi () as this =
167169
with get() = vslcid
168170
and set(p) = vslcid <- p
169171

172+
member fsc.LangVersion
173+
with get() = langVersion
174+
and set(s) = langVersion <- s
175+
170176
// --noframework
171177
member fsi.NoFramework
172178
with get() = noFramework

src/fsharp/LanguageFeatures.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type LanguageFeature =
2424
| SingleUnderscorePattern = 2
2525
| WildCardInForLoop = 3
2626
| RelaxWhitespace = 4
27+
| NameOf = 5
2728

2829
/// LanguageVersion management
2930
type LanguageVersion (specifiedVersion) =
@@ -47,8 +48,7 @@ type LanguageVersion (specifiedVersion) =
4748
LanguageFeature.SingleUnderscorePattern, previewVersion
4849
LanguageFeature.WildCardInForLoop, previewVersion
4950
LanguageFeature.RelaxWhitespace, previewVersion
50-
51-
// Add new LanguageFeatures here ...
51+
LanguageFeature.NameOf, previewVersion
5252
|]
5353

5454
let specified =

src/fsharp/LanguageFeatures.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type LanguageFeature =
1111
| SingleUnderscorePattern = 2
1212
| WildCardInForLoop = 3
1313
| RelaxWhitespace = 4
14+
| NameOf = 5
1415

1516
/// LanguageVersion management
1617
type LanguageVersion =

src/fsharp/NameResolution.fs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ open FSharp.Compiler.AttributeChecking
2424
open FSharp.Compiler.InfoReader
2525
open FSharp.Compiler.PrettyNaming
2626
open FSharp.Compiler.Text
27+
open FSharp.Compiler.Features
2728
open System.Collections.Generic
2829

2930
#if !NO_EXTENSIONTYPING
@@ -2444,7 +2445,16 @@ let rec ResolveExprLongIdentPrim sink (ncenv: NameResolver) first fullyQualified
24442445
| Exception e -> typeError := Some e; None
24452446

24462447
| true, res ->
2447-
Some (FreshenUnqualifiedItem ncenv m res, [])
2448+
let fresh = FreshenUnqualifiedItem ncenv m res
2449+
match fresh with
2450+
| Item.Value value ->
2451+
let isNameOfOperator = valRefEq ncenv.g ncenv.g.nameof_vref value
2452+
if isNameOfOperator && not (ncenv.g.langVersion.SupportsFeature LanguageFeature.NameOf) then
2453+
// Do not resolve `nameof` if the feature is unsupported, even if it is FSharp.Core
2454+
None
2455+
else
2456+
Some (fresh, [])
2457+
| _ -> Some (fresh, [])
24482458
| _ ->
24492459
None
24502460

src/fsharp/TastOps.fs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ open FSharp.Compiler.TcGlobals
2222
open FSharp.Compiler.Layout
2323
open FSharp.Compiler.Layout.TaggedTextOps
2424
open FSharp.Compiler.PrettyNaming
25+
open FSharp.Compiler.Features
2526
#if !NO_EXTENSIONTYPING
2627
open FSharp.Compiler.ExtensionTyping
2728
#endif
@@ -3206,10 +3207,10 @@ let isSizeOfValRef g vref =
32063207
// There is an internal version of typeof defined in prim-types.fs that needs to be detected
32073208
|| (g.compilingFslib && vref.LogicalName = "sizeof")
32083209

3209-
let isNameOfValRef g vref =
3210-
valRefEq g vref g.nameof_vref
3210+
let isNameOfValRef g vref =
3211+
valRefEq g vref g.nameof_vref
32113212
// There is an internal version of nameof defined in prim-types.fs that needs to be detected
3212-
|| (g.compilingFslib && vref.LogicalName = "nameof")
3213+
|| (g.compilingFslib && vref.LogicalName = "nameof")
32133214

32143215
let isTypeDefOfValRef g vref =
32153216
valRefEq g vref g.typedefof_vref
@@ -8568,8 +8569,8 @@ let IsSimpleSyntacticConstantExpr g inputExpr =
85688569
| Expr.Op (TOp.UnionCase _, _, [], _) // Nullary union cases
85698570
| UncheckedDefaultOfExpr g _
85708571
| SizeOfExpr g _
8571-
| TypeOfExpr g _
8572-
| NameOfExpr g _ -> true
8572+
| TypeOfExpr g _ -> true
8573+
| NameOfExpr g _ when g.langVersion.SupportsFeature LanguageFeature.NameOf -> true
85738574
// All others are not simple constant expressions
85748575
| _ -> false
85758576

src/fsharp/TcGlobals.fs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ open FSharp.Compiler.Range
2222
open FSharp.Compiler.Ast
2323
open FSharp.Compiler.Lib
2424
open FSharp.Compiler.PrettyNaming
25+
open FSharp.Compiler.Features
2526

2627
open Internal.Utilities
2728

@@ -180,8 +181,8 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
180181
// The helper to find system types amongst referenced DLLs
181182
tryFindSysTypeCcu,
182183
emitDebugInfoInQuotations: bool, noDebugData: bool,
183-
pathMap: PathMap) =
184-
184+
pathMap: PathMap, langVersion: LanguageVersion) =
185+
185186
let vara = NewRigidTypar "a" envRange
186187
let varb = NewRigidTypar "b" envRange
187188
let varc = NewRigidTypar "c" envRange
@@ -239,7 +240,7 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
239240
// Search for a type. If it is not found, leave a dangling CCU reference with some useful diagnostic information should
240241
// the type actually be dereferenced
241242
let findSysTypeCcu path typeName =
242-
match tryFindSysTypeCcu path typeName with
243+
match tryFindSysTypeCcu path typeName with
243244
| None -> CcuThunk.CreateDelayed(dummyAssemblyNameCarryingUsefulErrorInformation path typeName)
244245
| Some ccu -> ccu
245246

@@ -904,6 +905,7 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
904905
member __.emitDebugInfoInQuotations = emitDebugInfoInQuotations
905906
member __.directoryToResolveRelativePaths= directoryToResolveRelativePaths
906907
member __.pathMap = pathMap
908+
member __.langVersion = langVersion
907909
member __.unionCaseRefEq x y = primUnionCaseRefEq compilingFslib fslibCcu x y
908910
member __.valRefEq x y = primValRefEq compilingFslib fslibCcu x y
909911
member __.fslibCcu = fslibCcu

src/fsharp/TypeChecker.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ open FSharp.Compiler.ConstraintSolver
3636
open FSharp.Compiler.NameResolution
3737
open FSharp.Compiler.PrettyNaming
3838
open FSharp.Compiler.InfoReader
39+
open FSharp.Compiler.Features
3940

4041
#if !NO_EXTENSIONTYPING
4142
open FSharp.Compiler.ExtensionTyping
@@ -8762,9 +8763,9 @@ and TcFunctionApplicationThen cenv overallTy env tpenv mExprAndArg expr exprty (
87628763
// If the type of 'synArg' unifies as a function type, then this is a function application, otherwise
87638764
// it is an error or a computation expression
87648765
match UnifyFunctionTypeUndoIfFailed cenv denv mFunExpr exprty with
8765-
| ValueSome (domainTy, resultTy) ->
8766+
| ValueSome (domainTy, resultTy) ->
87668767
match expr with
8767-
| ApplicableExpr(_, NameOfExpr cenv.g _, _) ->
8768+
| ApplicableExpr(_, NameOfExpr cenv.g _, _) when cenv.g.langVersion.SupportsFeature LanguageFeature.NameOf ->
87688769
let replacementExpr = TcNameOfExpr cenv env tpenv synArg
87698770
TcDelayed cenv overallTy env tpenv mExprAndArg (ApplicableExpr(cenv, replacementExpr, true)) cenv.g.string_ty ExprAtomicFlag.Atomic delayed
87708771
| _ ->

tests/FSharp.Core.UnitTests/FSharp.Core.UnitTests.fsproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
<Compile Include="FSharp.Core\Microsoft.FSharp.Core\IntConversions.fs" />
6767
<Compile Include="FSharp.Core\Microsoft.FSharp.Core\IntConversionsGenerated.fs" />
6868
<None Include="FSharp.Core\Microsoft.FSharp.Core\IntConversionsTestGenerator.fsx" />
69-
<Compile Include="FSharp.Core\Microsoft.FSharp.Core\NameOfTests.fs" />
7069
<Compile Include="FSharp.Core\Microsoft.FSharp.Core\OptionModule.fs" />
7170
<Compile Include="FSharp.Core\Microsoft.FSharp.Core\PrintfTests.fs" />
7271
<Compile Include="FSharp.Core\Microsoft.FSharp.Core\ResultTests.fs" />

0 commit comments

Comments
 (0)