Skip to content

Commit

Permalink
Fix 15958: Missing warning FS3365 for SynLongIdent (dotless indexing …
Browse files Browse the repository at this point in the history
…vs application) (#16224)

* Add tests for FS3365

* improve tests

* add missing warning for 3365

* format

* refactor
  • Loading branch information
dawedawe authored Nov 6, 2023
1 parent 046ff38 commit a1ecaa1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Compiler/Checking/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3950,6 +3950,16 @@ let GetInstanceMemberThisVariable (vspec: Val, expr) =
else
None

/// c.atomicLeftMethExpr[idx] and atomicLeftExpr[idx] as applications give warnings
let checkHighPrecedenceFunctionApplicationToList (g: TcGlobals) args atomicFlag exprRange =
match args, atomicFlag with
| ([SynExpr.ArrayOrList (false, _, _)] | [SynExpr.ArrayOrListComputed (false, _, _)]), ExprAtomicFlag.Atomic ->
if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then
informationalWarning(Error(FSComp.SR.tcHighPrecedenceFunctionApplicationToListDeprecated(), exprRange))
elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then
informationalWarning(Error(FSComp.SR.tcHighPrecedenceFunctionApplicationToListReserved(), exprRange))
| _ -> ()

/// Indicates whether a syntactic type is allowed to include new type variables
/// not declared anywhere, e.g. `let f (x: 'T option) = x.Value`
type ImplicitlyBoundTyparsAllowed =
Expand Down Expand Up @@ -8202,13 +8212,7 @@ and TcApplicationThen (cenv: cenv) (overallTy: OverallTy) env tpenv mExprAndArg

// atomicLeftExpr[idx] unifying as application gives a warning
if not isSugar then
match synArg, atomicFlag with
| (SynExpr.ArrayOrList (false, _, _) | SynExpr.ArrayOrListComputed (false, _, _)), ExprAtomicFlag.Atomic ->
if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then
informationalWarning(Error(FSComp.SR.tcHighPrecedenceFunctionApplicationToListDeprecated(), mExprAndArg))
elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then
informationalWarning(Error(FSComp.SR.tcHighPrecedenceFunctionApplicationToListReserved(), mExprAndArg))
| _ -> ()
checkHighPrecedenceFunctionApplicationToList g [synArg] atomicFlag mExprAndArg

match leftExpr with
| ApplicableExpr(expr=NameOfExpr g _) when g.langVersion.SupportsFeature LanguageFeature.NameOf ->
Expand Down Expand Up @@ -9366,6 +9370,9 @@ and TcMethodApplicationThen
// Nb. args is always of List.length <= 1 except for indexed setters, when it is 2
let mWholeExpr = (m, args) ||> List.fold (fun m arg -> unionRanges m arg.Range)

// c.atomicLeftMethExpr[idx] as application gives a warning
checkHighPrecedenceFunctionApplicationToList g args atomicFlag mWholeExpr

// Work out if we know anything about the return type of the overall expression. If there are any delayed
// lookups then we don't know anything.
let exprTy = if isNil delayed then overallTy else MustEqual (NewInferenceType g)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace FSharp.Compiler.ComponentTests.ErrorMessages

open FSharp.Test.Compiler
open FSharp.Test.Compiler.Assertions.StructuredResultsAsserts

module ``Indexing Syntax`` =

[<FSharp.Test.FactForNETCOREAPP>]
let ``Warn successfully for SynExpr.Ident app`` () =
"""
namespace N
module M =
let f (a: int list) = a
let g () = f [1] // should not warn
let h () = f[1] // should warn
"""
|> FSharp
|> withLangVersion70
|> compile
|> shouldFail
|> withResults
[
{
Error = Information 3365
Range =
{
StartLine = 10
StartColumn = 20
EndLine = 10
EndColumn = 24
}
Message =
"The syntax 'expr1[expr2]' is used for indexing. Consider adding a type annotation to enable indexing, or if calling a function add a space, e.g. 'expr1 [expr2]'."
}
]

[<FSharp.Test.FactForNETCOREAPP>]
let ``Warn successfully for SynExpr.LongIdent app`` () =
"""
namespace N
module N =
type C () =
member _.MyFunc (inputList: int list) = inputList
let g () =
let c = C()
let _ = c.MyFunc [23] // should not warn
c.MyFunc[42] // should warn
"""
|> FSharp
|> withLangVersion70
|> compile
|> shouldFail
|> withResults
[
{
Error = Information 3365
Range =
{
StartLine = 12
StartColumn = 13
EndLine = 12
EndColumn = 25
}
Message =
"The syntax 'expr1[expr2]' is used for indexing. Consider adding a type annotation to enable indexing, or if calling a function add a space, e.g. 'expr1 [expr2]'."
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<Compile Include="EmittedIL\FixedBindings\FixedBindings.fs" />
<Compile Include="ErrorMessages\UnsupportedAttributes.fs" />
<Compile Include="ErrorMessages\TailCallAttribute.fs" />
<Compile Include="ErrorMessages\IndexingSyntax.fs" />
<Compile Include="ErrorMessages\TypeEqualsMissingTests.fs" />
<Compile Include="ErrorMessages\AccessOfTypeAbbreviationTests.fs" />
<Compile Include="ErrorMessages\AssignmentErrorTests.fs" />
Expand Down

0 comments on commit a1ecaa1

Please sign in to comment.