Skip to content

Commit bde3682

Browse files
authored
Fix suggestions (#7311)
* Fix suggestions * feedback
1 parent 6e46c5a commit bde3682

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/fsharp/NameResolution.fs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type NameResolver(g: TcGlobals,
3636
amap: Import.ImportMap,
3737
infoReader: InfoReader,
3838
instantiationGenerator: (range -> Typars -> TypeInst)) =
39+
3940
/// Used to transform typars into new inference typars
4041
// instantiationGenerator is a function to help us create the
4142
// type parameters by copying them from type parameter specifications read
@@ -50,6 +51,7 @@ type NameResolver(g: TcGlobals,
5051
member nr.g = g
5152
member nr.amap = amap
5253
member nr.InfoReader = infoReader
54+
member nr.languageSupportsNameOf = g.langVersion.SupportsFeature LanguageFeature.NameOf
5355

5456
//-------------------------------------------------------------------------
5557
// Helpers for unionconstrs and recdfields
@@ -2496,6 +2498,15 @@ let ChooseTyconRefInExpr (ncenv: NameResolver, m, ad, nenv, id: Ident, typeNameR
24962498
/// that may represent further actions, e.g. further lookups.
24972499
let rec ResolveExprLongIdentPrim sink (ncenv: NameResolver) first fullyQualified m ad nenv (typeNameResInfo: TypeNameResolutionInfo) (id: Ident) (rest: Ident list) isOpenDecl =
24982500
let resInfo = ResolutionInfo.Empty
2501+
let canSuggestThisItem (item:Item) =
2502+
// All items can be suggested except nameof when it comes from FSharp.Core.dll and the nameof feature is not enabled
2503+
match item with
2504+
| Item.Value v ->
2505+
let isNameOfOperator = valRefEq ncenv.g ncenv.g.nameof_vref v
2506+
if isNameOfOperator && not (ncenv.g.langVersion.SupportsFeature LanguageFeature.NameOf) then false
2507+
else true
2508+
| _ -> true
2509+
24992510
if first && id.idText = MangledGlobalName then
25002511
match rest with
25012512
| [] ->
@@ -2534,7 +2545,7 @@ let rec ResolveExprLongIdentPrim sink (ncenv: NameResolver) first fullyQualified
25342545
match fresh with
25352546
| Item.Value value ->
25362547
let isNameOfOperator = valRefEq ncenv.g ncenv.g.nameof_vref value
2537-
if isNameOfOperator && not (ncenv.g.langVersion.SupportsFeature LanguageFeature.NameOf) then
2548+
if isNameOfOperator && not (ncenv.languageSupportsNameOf) then
25382549
// Do not resolve `nameof` if the feature is unsupported, even if it is FSharp.Core
25392550
None
25402551
else
@@ -2570,7 +2581,8 @@ let rec ResolveExprLongIdentPrim sink (ncenv: NameResolver) first fullyQualified
25702581
| _ ->
25712582
let suggestNamesAndTypes (addToBuffer: string -> unit) =
25722583
for e in nenv.eUnqualifiedItems do
2573-
addToBuffer e.Value.DisplayName
2584+
if canSuggestThisItem e.Value then
2585+
addToBuffer e.Value.DisplayName
25742586

25752587
for e in nenv.TyconsByDemangledNameAndArity fullyQualified do
25762588
if IsEntityAccessible ncenv.amap m ad e.Value then
@@ -2666,7 +2678,8 @@ let rec ResolveExprLongIdentPrim sink (ncenv: NameResolver) first fullyQualified
26662678
addToBuffer e.Value.DisplayName
26672679

26682680
for e in nenv.eUnqualifiedItems do
2669-
addToBuffer e.Value.DisplayName
2681+
if canSuggestThisItem e.Value then
2682+
addToBuffer e.Value.DisplayName
26702683

26712684
match innerSearch with
26722685
| Exception (UndefinedName(0, _, id1, suggestionsF)) when Range.equals id.idRange id1.idRange ->

src/fsharp/NameResolution.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type NameResolver =
2222
member InfoReader : InfoReader
2323
member amap : ImportMap
2424
member g : TcGlobals
25+
member languageSupportsNameOf : bool
2526

2627
/// Get the active pattern elements defined in a module, if any. Cache in the slot in the module type.
2728
val ActivePatternElemsOfModuleOrNamespace : ModuleOrNamespaceRef -> NameMap<ActivePatternElemRef>

tests/fsharp/core/nameof/version46/test.fsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747
//<Expects id="FS0039" status="error" span="(322,17)">The value or constructor 'nameof' is not defined.</Expects>
4848
//<Expects id="FS0039" status="error" span="(336,22)">The value or constructor 'nameof' is not defined.</Expects>
4949
//<Expects id="FS0039" status="error" span="(337,22)">The value or constructor 'nameof' is not defined.</Expects>
50+
//<Expects id="FS0039" status="error" span="(342,13)">The value or constructor 'name' is not defined. Maybe you want one of the following:</Expects>
51+
5052

5153

5254
#if TESTS_AS_APP
5355
module TestSuite_FSharpCore_nameof_46
5456
#endif
55-
56-
5757
#nowarn "44"
5858

5959
open System
@@ -337,6 +337,10 @@ type Person =
337337
| x when x = nameof __.Age -> { __ with Age = value :?> int }
338338
| _ -> __
339339

340+
module Foo =
341+
let nameof = ()
342+
let x = name ()
343+
340344
do test "local variable name lookup" (BasicNameOfTests.``local variable name lookup`` ())
341345
do test "local int function name" (BasicNameOfTests.``local int function name`` ())
342346
do test "local curried function name" (BasicNameOfTests.``local curried function name`` ())

0 commit comments

Comments
 (0)