Skip to content

Commit 67f160c

Browse files
edgarfgpbrianrourkebollpsfinaki
authored
Better error reporting for let bindings. (#17601)
* Use `SynPat` range for let binding errors * update error range for tcAttributesAreNotPermittedOnLetBindings * Include typars * AP better error message * fantomas * Fix tests * fantomas * update tests * Add a new compiler error for multi-case partial active patterns are not supported * use tcPartialActivePattern and add more tests * update tests * Update src/Compiler/FSComp.txt Co-authored-by: Brian Rourke Boll <[email protected]> * Update FSComp.txt * Update src/Compiler/FSComp.txt Co-authored-by: Brian Rourke Boll <[email protected]> * more tests * release notes * Update xlf * reduce diff * update tests * baselines * move `neg16.bsl` content to different tests * move `neg16.bsl` content to different tests * Update bsl * Update last failing tests. * Update last failing tests. * FactForDESKTOP neg16 * fix bsl --------- Co-authored-by: Brian Rourke Boll <[email protected]> Co-authored-by: psfinaki <[email protected]>
1 parent 5f936eb commit 67f160c

File tree

49 files changed

+473
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+473
-153
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.100.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
* Applied nullable reference types to FSharp.Compiler.Service itself ([PR #15310](https://github.com/dotnet/fsharp/pull/15310))
4343
* Ensure that isinteractive multi-emit backing fields are not public. ([Issue #17439](https://github.com/dotnet/fsharp/issues/17438)), ([PR #17439](https://github.com/dotnet/fsharp/pull/17439))
4444
* Better error reporting for unions with duplicated fields. ([PR #17521](https://github.com/dotnet/fsharp/pull/17521))
45+
* Better error reporting for let bindings. ([PR #17601](https://github.com/dotnet/fsharp/pull/17601))
4546
* Optimize ILTypeDef interface impls reading from metadata. ([PR #17382](https://github.com/dotnet/fsharp/pull/17382))
4647

48+
4749
### Breaking Changes

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,7 @@ module GeneralizationHelpers =
22742274
// to C<_> occurs then generate C<?ty> for a fresh type inference variable ?ty.
22752275
//-------------------------------------------------------------------------
22762276

2277-
let CheckDeclaredTyparsPermitted (memFlagsOpt: SynMemberFlags option, declaredTypars, m) =
2277+
let CheckDeclaredTyparsPermitted (memFlagsOpt: SynMemberFlags option, declaredTypars: Typars, m) =
22782278
match memFlagsOpt with
22792279
| None -> ()
22802280
| Some memberFlags ->
@@ -2284,7 +2284,13 @@ module GeneralizationHelpers =
22842284
| SynMemberKind.PropertySet
22852285
| SynMemberKind.PropertyGetSet ->
22862286
if not (isNil declaredTypars) then
2287-
errorR(Error(FSComp.SR.tcPropertyRequiresExplicitTypeParameters(), m))
2287+
let declaredTyparsRange =
2288+
declaredTypars
2289+
|> List.map(fun typar -> typar.Range)
2290+
2291+
let m = declaredTyparsRange |> List.fold (fun r a -> unionRanges r a) range.Zero
2292+
2293+
errorR(Error(FSComp.SR.tcPropertyRequiresExplicitTypeParameters(), m))
22882294
| SynMemberKind.Constructor ->
22892295
if not (isNil declaredTypars) then
22902296
errorR(Error(FSComp.SR.tcConstructorCannotHaveTypeParameters(), m))
@@ -10816,8 +10822,9 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
1081610822
let envinner = AddDeclaredTypars NoCheckForDuplicateTypars (enclosingDeclaredTypars@declaredTypars) env
1081710823

1081810824
match bind with
10819-
| NormalizedBinding(vis, kind, isInline, isMutable, attrs, xmlDoc, _, valSynData, pat, NormalizedBindingRhs(spatsL, rtyOpt, rhsExpr), mBinding, debugPoint) ->
10825+
| NormalizedBinding(vis, kind, isInline, isMutable, attrs, xmlDoc, _, valSynData, pat, NormalizedBindingRhs(spatsL, rtyOpt, rhsExpr), _, debugPoint) ->
1082010826
let (SynValData(memberFlags = memberFlagsOpt)) = valSynData
10827+
let mBinding = pat.Range
1082110828

1082210829
let isClassLetBinding =
1082310830
match declKind, kind with
@@ -10863,8 +10870,10 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
1086310870
// targeting the return value.
1086410871
let tgtEx = if isRet then enum 0 else AttributeTargets.ReturnValue
1086510872
let attrs, _ = TcAttributesMaybeFailEx false cenv envinner tgt tgtEx attrs
10873+
let attrs: Attrib list = attrs
1086610874
if attrTgt = enum 0 && not (isNil attrs) then
10867-
errorR(Error(FSComp.SR.tcAttributesAreNotPermittedOnLetBindings(), mBinding))
10875+
for attr in attrs do
10876+
errorR(Error(FSComp.SR.tcAttributesAreNotPermittedOnLetBindings(), attr.Range))
1086810877
attrs
1086910878

1087010879
// Rotate [<return:...>] from binding to return value
@@ -10984,8 +10993,12 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
1098410993
let envinner =
1098510994
match apinfoOpt with
1098610995
| Some (apinfo, apOverallTy, m) ->
10987-
if Option.isSome memberFlagsOpt || (not apinfo.IsTotal && apinfo.ActiveTags.Length > 1) then
10988-
error(Error(FSComp.SR.tcInvalidActivePatternName(), mBinding))
10996+
let isMultiCasePartialAP = memberFlagsOpt.IsNone && not apinfo.IsTotal && apinfo.ActiveTags.Length > 1
10997+
if isMultiCasePartialAP then
10998+
errorR(Error(FSComp.SR.tcPartialActivePattern(), m))
10999+
11000+
if Option.isSome memberFlagsOpt && not spatsL.IsEmpty then
11001+
errorR(Error(FSComp.SR.tcInvalidActivePatternName(apinfo.LogicalName), m))
1098911002

1099011003
apinfo.ActiveTagsWithRanges |> List.iteri (fun i (_tag, tagRange) ->
1099111004
let item = Item.ActivePatternResult(apinfo, apOverallTy, i, tagRange)
@@ -12715,8 +12728,9 @@ let TcAndPublishValSpec (cenv: cenv, env, containerInfo: ContainerInfo, declKind
1271512728

1271612729
let (SynValSig (attributes=Attributes synAttrs; explicitTypeParams=explicitTypeParams; isInline=isInline; isMutable=mutableFlag; xmlDoc=xmlDoc; accessibility=vis; synExpr=literalExprOpt; range=m)) = synValSig
1271712730
let (ValTyparDecls (synTypars, _, synCanInferTypars)) = explicitTypeParams
12731+
let declaredTypars = TcTyparDecls cenv env synTypars
1271812732

12719-
GeneralizationHelpers.CheckDeclaredTyparsPermitted(memFlagsOpt, synTypars, m)
12733+
GeneralizationHelpers.CheckDeclaredTyparsPermitted(memFlagsOpt, declaredTypars, m)
1272012734

1272112735
let canInferTypars = GeneralizationHelpers.ComputeCanInferExtraGeneralizableTypars (containerInfo.ParentRef, synCanInferTypars, memFlagsOpt)
1272212736

src/Compiler/FSComp.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ tcUnnamedArgumentsDoNotFormPrefix,"The unnamed arguments do not form a prefix of
679679
824,tcAttributesAreNotPermittedOnLetBindings,"Attributes are not permitted on 'let' bindings in expressions"
680680
825,tcDefaultValueAttributeRequiresVal,"The 'DefaultValue' attribute may only be used on 'val' declarations"
681681
826,tcConditionalAttributeRequiresMembers,"The 'ConditionalAttribute' attribute may only be used on members"
682-
827,tcInvalidActivePatternName,"This is not a valid name for an active pattern"
682+
827,tcInvalidActivePatternName,"'%s' is not a valid method name. Use a 'let' binding instead."
683683
828,tcEntryPointAttributeRequiresFunctionInModule,"The 'EntryPointAttribute' attribute may only be used on function definitions in modules"
684684
829,tcMutableValuesCannotBeInline,"Mutable values cannot be marked 'inline'"
685685
830,tcMutableValuesMayNotHaveGenericParameters,"Mutable values cannot have generic parameters"
@@ -1783,3 +1783,4 @@ featureEmptyBodiedComputationExpressions,"Support for computation expressions wi
17831783
featureAllowAccessModifiersToAutoPropertiesGettersAndSetters,"Allow access modifiers to auto properties getters and setters"
17841784
3871,tcAccessModifiersNotAllowedInSRTPConstraint,"Access modifiers cannot be applied to an SRTP constraint."
17851785
featureAllowObjectExpressionWithoutOverrides,"Allow object expressions without overrides"
1786+
3872,tcPartialActivePattern,"Multi-case partial active patterns are not supported. Consider using a single-case partial active pattern or a full active pattern."

src/Compiler/SyntaxTree/PrettyNaming.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,14 @@ type ActivePatternInfo =
963963

964964
member x.ActiveTagsWithRanges = let (APInfo(_, tags, _)) = x in tags
965965

966+
member x.LogicalName =
967+
let (APInfo(isTotal, tags, _)) = x
968+
969+
tags
970+
|> List.map fst
971+
|> String.concat "|"
972+
|> (fun s -> if isTotal then "(|" + s + "|)" else "(|" + s + "|_|)")
973+
966974
member x.Range = let (APInfo(_, _, m)) = x in m
967975

968976
let ActivePatternInfoOfValName nm (m: range) =

src/Compiler/SyntaxTree/PrettyNaming.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ type internal ActivePatternInfo =
228228

229229
member ActiveTags: string list
230230
member ActiveTagsWithRanges: (string * range) list
231+
member LogicalName: string
231232
member IsTotal: bool
232233
member Range: range
233234

src/Compiler/xlf/FSComp.txt.cs.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.de.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.es.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.fr.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.it.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)