Skip to content

Commit

Permalink
Better error reporting for let bindings. (#17601)
Browse files Browse the repository at this point in the history
* 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 <brianrourkeboll@users.noreply.github.com>

* Update FSComp.txt

* Update src/Compiler/FSComp.txt

Co-authored-by: Brian Rourke Boll <brianrourkeboll@users.noreply.github.com>

* 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 <brianrourkeboll@users.noreply.github.com>
Co-authored-by: psfinaki <psfinaki@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 5, 2024
1 parent 5f936eb commit 67f160c
Show file tree
Hide file tree
Showing 49 changed files with 473 additions and 153 deletions.
2 changes: 2 additions & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.100.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
* Applied nullable reference types to FSharp.Compiler.Service itself ([PR #15310](https://github.com/dotnet/fsharp/pull/15310))
* 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))
* Better error reporting for unions with duplicated fields. ([PR #17521](https://github.com/dotnet/fsharp/pull/17521))
* Better error reporting for let bindings. ([PR #17601](https://github.com/dotnet/fsharp/pull/17601))
* Optimize ILTypeDef interface impls reading from metadata. ([PR #17382](https://github.com/dotnet/fsharp/pull/17382))


### Breaking Changes
28 changes: 21 additions & 7 deletions src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ module GeneralizationHelpers =
// to C<_> occurs then generate C<?ty> for a fresh type inference variable ?ty.
//-------------------------------------------------------------------------

let CheckDeclaredTyparsPermitted (memFlagsOpt: SynMemberFlags option, declaredTypars, m) =
let CheckDeclaredTyparsPermitted (memFlagsOpt: SynMemberFlags option, declaredTypars: Typars, m) =
match memFlagsOpt with
| None -> ()
| Some memberFlags ->
Expand All @@ -2284,7 +2284,13 @@ module GeneralizationHelpers =
| SynMemberKind.PropertySet
| SynMemberKind.PropertyGetSet ->
if not (isNil declaredTypars) then
errorR(Error(FSComp.SR.tcPropertyRequiresExplicitTypeParameters(), m))
let declaredTyparsRange =
declaredTypars
|> List.map(fun typar -> typar.Range)

let m = declaredTyparsRange |> List.fold (fun r a -> unionRanges r a) range.Zero

errorR(Error(FSComp.SR.tcPropertyRequiresExplicitTypeParameters(), m))
| SynMemberKind.Constructor ->
if not (isNil declaredTypars) then
errorR(Error(FSComp.SR.tcConstructorCannotHaveTypeParameters(), m))
Expand Down Expand Up @@ -10816,8 +10822,9 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
let envinner = AddDeclaredTypars NoCheckForDuplicateTypars (enclosingDeclaredTypars@declaredTypars) env

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

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

// Rotate [<return:...>] from binding to return value
Expand Down Expand Up @@ -10984,8 +10993,12 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
let envinner =
match apinfoOpt with
| Some (apinfo, apOverallTy, m) ->
if Option.isSome memberFlagsOpt || (not apinfo.IsTotal && apinfo.ActiveTags.Length > 1) then
error(Error(FSComp.SR.tcInvalidActivePatternName(), mBinding))
let isMultiCasePartialAP = memberFlagsOpt.IsNone && not apinfo.IsTotal && apinfo.ActiveTags.Length > 1
if isMultiCasePartialAP then
errorR(Error(FSComp.SR.tcPartialActivePattern(), m))

if Option.isSome memberFlagsOpt && not spatsL.IsEmpty then
errorR(Error(FSComp.SR.tcInvalidActivePatternName(apinfo.LogicalName), m))

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

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

GeneralizationHelpers.CheckDeclaredTyparsPermitted(memFlagsOpt, synTypars, m)
GeneralizationHelpers.CheckDeclaredTyparsPermitted(memFlagsOpt, declaredTypars, m)

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

Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ tcUnnamedArgumentsDoNotFormPrefix,"The unnamed arguments do not form a prefix of
824,tcAttributesAreNotPermittedOnLetBindings,"Attributes are not permitted on 'let' bindings in expressions"
825,tcDefaultValueAttributeRequiresVal,"The 'DefaultValue' attribute may only be used on 'val' declarations"
826,tcConditionalAttributeRequiresMembers,"The 'ConditionalAttribute' attribute may only be used on members"
827,tcInvalidActivePatternName,"This is not a valid name for an active pattern"
827,tcInvalidActivePatternName,"'%s' is not a valid method name. Use a 'let' binding instead."
828,tcEntryPointAttributeRequiresFunctionInModule,"The 'EntryPointAttribute' attribute may only be used on function definitions in modules"
829,tcMutableValuesCannotBeInline,"Mutable values cannot be marked 'inline'"
830,tcMutableValuesMayNotHaveGenericParameters,"Mutable values cannot have generic parameters"
Expand Down Expand Up @@ -1783,3 +1783,4 @@ featureEmptyBodiedComputationExpressions,"Support for computation expressions wi
featureAllowAccessModifiersToAutoPropertiesGettersAndSetters,"Allow access modifiers to auto properties getters and setters"
3871,tcAccessModifiersNotAllowedInSRTPConstraint,"Access modifiers cannot be applied to an SRTP constraint."
featureAllowObjectExpressionWithoutOverrides,"Allow object expressions without overrides"
3872,tcPartialActivePattern,"Multi-case partial active patterns are not supported. Consider using a single-case partial active pattern or a full active pattern."
8 changes: 8 additions & 0 deletions src/Compiler/SyntaxTree/PrettyNaming.fs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,14 @@ type ActivePatternInfo =

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

member x.LogicalName =
let (APInfo(isTotal, tags, _)) = x

tags
|> List.map fst
|> String.concat "|"
|> (fun s -> if isTotal then "(|" + s + "|)" else "(|" + s + "|_|)")

member x.Range = let (APInfo(_, _, m)) = x in m

let ActivePatternInfoOfValName nm (m: range) =
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/SyntaxTree/PrettyNaming.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ type internal ActivePatternInfo =

member ActiveTags: string list
member ActiveTagsWithRanges: (string * range) list
member LogicalName: string
member IsTotal: bool
member Range: range

Expand Down
9 changes: 7 additions & 2 deletions src/Compiler/xlf/FSComp.txt.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/Compiler/xlf/FSComp.txt.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/Compiler/xlf/FSComp.txt.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/Compiler/xlf/FSComp.txt.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/Compiler/xlf/FSComp.txt.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/Compiler/xlf/FSComp.txt.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/Compiler/xlf/FSComp.txt.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 67f160c

Please sign in to comment.