Skip to content

Commit 013a716

Browse files
authored
use errorR instead of error in CheckDeclarations when possible. (#18645)
1 parent 6f0d144 commit 013a716

File tree

6 files changed

+41
-29
lines changed

6 files changed

+41
-29
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Added
22
* Add opt-in warning attribute not valid for union case with fields [PR #18532](https://github.com/dotnet/fsharp/pull/18532))
3+
* Add support for `when 'T : Enum` library-only static optimization constraint. ([PR #18546](https://github.com/dotnet/fsharp/pull/18546))
34

45
### Fixed
56

@@ -12,9 +13,8 @@
1213
* Completion: fix previous namespace considered opened [PR #18609](https://github.com/dotnet/fsharp/pull/18609)
1314
* Fix active pattern typechecking regression. ([Issue #18638](https://github.com/dotnet/fsharp/issues/18638), [PR #18642](https://github.com/dotnet/fsharp/pull/18642))
1415

15-
### Added
16-
17-
* Add support for `when 'T : Enum` library-only library-only static optimization constraint. ([PR #18546](https://github.com/dotnet/fsharp/pull/18546))
16+
### Changed
17+
* Use `errorR` instead of `error` in `CheckDeclarations.fs` when possible. ([PR #18645](https://github.com/dotnet/fsharp/pull/18645))
1818

1919
### Breaking Changes
2020

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ module TcRecdUnionAndEnumDeclarations =
441441

442442
let isThreadStatic = isThreadOrContextStatic g attrsForField
443443
if isThreadStatic && (not zeroInit || not isStatic) then
444-
error(Error(FSComp.SR.tcThreadStaticAndContextStaticMustBeStatic(), m))
444+
errorR(Error(FSComp.SR.tcThreadStaticAndContextStaticMustBeStatic(), m))
445445

446446
if isVolatile then
447-
error(Error(FSComp.SR.tcVolatileOnlyOnClassLetBindings(), m))
447+
errorR(Error(FSComp.SR.tcVolatileOnlyOnClassLetBindings(), m))
448448

449449
if isIncrClass && (not zeroInit || not isMutable) then errorR(Error(FSComp.SR.tcUninitializedValFieldsMustBeMutable(), m))
450450
let isPrivate = match vis with | Some (SynAccess.Private _) -> true | _ -> false
@@ -566,7 +566,7 @@ module TcRecdUnionAndEnumDeclarations =
566566
MakeRecdFieldSpec g env parent (false, None, argTy, [], [], id, argInfo.Name.IsNone, false, false, XmlDoc.Empty, None, m))
567567

568568
if not (typeEquiv g recordTy thisTy) then
569-
error(Error(FSComp.SR.tcReturnTypesForUnionMustBeSameAsType(), m))
569+
errorR(Error(FSComp.SR.tcReturnTypesForUnionMustBeSameAsType(), m))
570570
rfields, recordTy
571571

572572
let names = rfields
@@ -756,10 +756,10 @@ let TcOpenTypeDecl (cenv: cenv) mOpenDecl scopem env (synType: SynType, m) =
756756
let ty, _tpenv = TcType cenv NoNewTypars CheckCxs ItemOccurrence.Open WarnOnIWSAM.Yes env emptyUnscopedTyparEnv synType
757757

758758
if not (isAppTy g ty) then
759-
error(Error(FSComp.SR.tcNamedTypeRequired("open type"), m))
759+
errorR(Error(FSComp.SR.tcNamedTypeRequired("open type"), m))
760760

761761
if isByrefTy g ty then
762-
error(Error(FSComp.SR.tcIllegalByrefsInOpenTypeDeclaration(), m))
762+
errorR(Error(FSComp.SR.tcIllegalByrefsInOpenTypeDeclaration(), m))
763763

764764
let openDecl = OpenDeclaration.Create (SynOpenDeclTarget.Type (synType, m), [], [ty], scopem, false)
765765
let env = OpenTypeContent cenv.tcSink g cenv.amap scopem env ty openDecl
@@ -1065,11 +1065,11 @@ module MutRecBindingChecking =
10651065

10661066
if tcref.IsTypeAbbrev then
10671067
// ideally we'd have the 'm' of the type declaration stored here, to avoid needing to trim to line to approx
1068-
error(Error(FSComp.SR.tcTypeAbbreviationsMayNotHaveMembers(), (trimRangeToLine m)))
1068+
errorR(Error(FSComp.SR.tcTypeAbbreviationsMayNotHaveMembers(), (trimRangeToLine m)))
10691069

10701070
if tcref.IsEnumTycon && (declKind <> ExtrinsicExtensionBinding) && classMemberDef.IsSome then
10711071
// ideally we'd have the 'm' of the type declaration stored here, to avoid needing to trim to line to approx
1072-
error(Error(FSComp.SR.tcEnumerationsMayNotHaveMembers(), (trimRangeToLine m)))
1072+
errorR(Error(FSComp.SR.tcEnumerationsMayNotHaveMembers(), (trimRangeToLine m)))
10731073

10741074
match classMemberDef, containerInfo with
10751075

@@ -1086,7 +1086,7 @@ module MutRecBindingChecking =
10861086
let (MemberOrValContainerInfo(tcref, _, baseValOpt, safeInitInfo, _)) = memberContainerInfo
10871087

10881088
if tcref.TypeOrMeasureKind = TyparKind.Measure then
1089-
error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
1089+
errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
10901090

10911091
// Phase2A: make staticCtorInfo - ctorv, thisVal etc, type depends on argty(s)
10921092
let staticCtorInfo = TcStaticImplicitCtorInfo_Phase2A(cenv, envForTycon, tcref, m, copyOfTyconTypars)
@@ -1102,7 +1102,7 @@ module MutRecBindingChecking =
11021102

11031103
| Some (SynMemberDefn.ImplicitInherit (ty, arg, _baseIdOpt, m, _)), _ ->
11041104
if tcref.TypeOrMeasureKind = TyparKind.Measure then
1105-
error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
1105+
errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
11061106

11071107
// Phase2A: inherit ty(arg) as base - pass through
11081108
// Phase2A: pick up baseValOpt!
@@ -1112,7 +1112,7 @@ module MutRecBindingChecking =
11121112

11131113
| Some (SynMemberDefn.LetBindings (letBinds, isStatic, isRec, m)), _ ->
11141114
match tcref.TypeOrMeasureKind, isStatic with
1115-
| TyparKind.Measure, false -> error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
1115+
| TyparKind.Measure, false -> errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
11161116
| _ -> ()
11171117

11181118
if not isStatic && tcref.IsStructOrEnumTycon then
@@ -1149,9 +1149,9 @@ module MutRecBindingChecking =
11491149
match memberFlagsOpt with
11501150
| None -> ()
11511151
| Some memberFlags ->
1152-
if memberFlags.IsInstance then error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
1152+
if memberFlags.IsInstance then errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m))
11531153
match memberFlags.MemberKind with
1154-
| SynMemberKind.Constructor -> error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembersNotConstructors(), m))
1154+
| SynMemberKind.Constructor -> errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembersNotConstructors(), m))
11551155
| _ -> ()
11561156

11571157
let envForMember =
@@ -1880,7 +1880,7 @@ module MutRecBindingChecking =
18801880
for extraTypar in allExtraGeneralizableTypars do
18811881
if Zset.memberOf freeInInitialEnv extraTypar then
18821882
let ty = mkTyparTy extraTypar
1883-
error(Error(FSComp.SR.tcNotSufficientlyGenericBecauseOfScope(NicePrint.prettyStringOfTy denv ty), extraTypar.Range))
1883+
errorR(Error(FSComp.SR.tcNotSufficientlyGenericBecauseOfScope(NicePrint.prettyStringOfTy denv ty), extraTypar.Range))
18841884

18851885
// Solve any type variables in any part of the overall type signature of the class whose
18861886
// constraints involve generalized type variables.
@@ -1980,8 +1980,8 @@ let TcMutRecDefns_Phase2 (cenv: cenv) envInitial mBinds scopem mutRecNSInfo (env
19801980
| SynMemberDefn.Interface(interfaceType=intfTy; members=defnOpt) ->
19811981
let ty = if tcref.Deref.IsFSharpException then g.exn_ty else generalizedTyconRef g tcref
19821982
let m = intfTy.Range
1983-
if tcref.IsTypeAbbrev then error(Error(FSComp.SR.tcTypeAbbreviationsCannotHaveInterfaceDeclaration(), m))
1984-
if tcref.IsEnumTycon then error(Error(FSComp.SR.tcEnumerationsCannotHaveInterfaceDeclaration(), m))
1983+
if tcref.IsTypeAbbrev then errorR(Error(FSComp.SR.tcTypeAbbreviationsCannotHaveInterfaceDeclaration(), m))
1984+
if tcref.IsEnumTycon then errorR(Error(FSComp.SR.tcEnumerationsCannotHaveInterfaceDeclaration(), m))
19851985

19861986
let intfTyR =
19871987
let envinner = AddDeclaredTypars CheckForDuplicateTypars declaredTyconTypars envForTycon
@@ -2053,7 +2053,7 @@ let TcMutRecDefns_Phase2 (cenv: cenv) envInitial mBinds scopem mutRecNSInfo (env
20532053
| SynMemberDefn.AbstractSlot _
20542054
| SynMemberDefn.ValField _
20552055
| SynMemberDefn.Inherit _ -> error(InternalError("Unexpected declaration element", memb.Range))
2056-
| SynMemberDefn.NestedType _ -> error(Error(FSComp.SR.tcTypesCannotContainNestedTypes(), memb.Range)) ]
2056+
| SynMemberDefn.NestedType _ -> errorR(Error(FSComp.SR.tcTypesCannotContainNestedTypes(), memb.Range)) ]
20572057

20582058
let tpenv = emptyUnscopedTyparEnv
20592059

@@ -2381,7 +2381,7 @@ module TyconConstraintInference =
23812381
//-------------------------------------------------------------------------
23822382

23832383
let ComputeModuleName (longPath: Ident list) =
2384-
if longPath.Length <> 1 then error(Error(FSComp.SR.tcInvalidModuleName(), (List.head longPath).idRange))
2384+
if longPath.Length <> 1 then errorR(Error(FSComp.SR.tcInvalidModuleName(), (List.head longPath).idRange))
23852385
longPath.Head
23862386

23872387
let CheckForDuplicateConcreteType env nm m =
@@ -2418,7 +2418,12 @@ module TcExceptionDeclarations =
24182418

24192419
let TcExnDefnCore_Phase1G_EstablishRepresentation (cenv: cenv) (env: TcEnv) parent (exnc: Entity) (SynExceptionDefnRepr(_, SynUnionCase(caseType=args), reprIdOpt, _, _, m)) =
24202420
let g = cenv.g
2421-
let args = match args with SynUnionCaseKind.Fields args -> args | _ -> error(Error(FSComp.SR.tcExplicitTypeSpecificationCannotBeUsedForExceptionConstructors(), m))
2421+
let args =
2422+
match args with
2423+
| SynUnionCaseKind.Fields args -> args
2424+
| _ ->
2425+
errorR(Error(FSComp.SR.tcExplicitTypeSpecificationCannotBeUsedForExceptionConstructors(), m))
2426+
[]
24222427
let ad = env.AccessRights
24232428
let id = exnc.Id
24242429

@@ -2450,7 +2455,7 @@ module TcExceptionDeclarations =
24502455
// REVIEW: check this really is an exception type
24512456
match args' with
24522457
| [] -> ()
2453-
| _ -> error (Error(FSComp.SR.tcAbbreviationsFordotNetExceptionsCannotTakeArguments(), m))
2458+
| _ -> errorR (Error(FSComp.SR.tcAbbreviationsFordotNetExceptionsCannotTakeArguments(), m))
24542459
let candidates =
24552460
meths |> List.filter (fun minfo ->
24562461
minfo.NumArgs = [args'.Length] &&
@@ -2545,7 +2550,7 @@ module EstablishTypeDefinitionCores =
25452550

25462551
/// Compute the mangled name of a type definition. 'doErase' is true for all type definitions except type abbreviations.
25472552
let private ComputeTyconName (longPath: Ident list, doErase: bool, typars: Typars) =
2548-
if longPath.Length <> 1 then error(Error(FSComp.SR.tcInvalidTypeExtension(), longPath.Head.idRange))
2553+
if longPath.Length <> 1 then errorR(Error(FSComp.SR.tcInvalidTypeExtension(), longPath.Head.idRange))
25492554
let id = longPath.Head
25502555
let erasedArity =
25512556
if doErase then typars |> Seq.sumBy (fun tp -> if tp.IsErased then 0 else 1)
@@ -2569,7 +2574,7 @@ module EstablishTypeDefinitionCores =
25692574
let bi b = (if b then 1 else 0)
25702575
if (bi hasClassAttr + bi hasInterfaceAttr + bi hasStructAttr + bi hasMeasureAttr) > 1 ||
25712576
(bi hasAbstractClassAttr + bi hasInterfaceAttr + bi hasStructAttr + bi hasMeasureAttr) > 1 then
2572-
error(Error(FSComp.SR.tcAttributesOfTypeSpecifyMultipleKindsForType(), m))
2577+
errorR(Error(FSComp.SR.tcAttributesOfTypeSpecifyMultipleKindsForType(), m))
25732578

25742579
match kind with
25752580
| SynTypeDefnKind.Unspecified ->
@@ -2584,7 +2589,7 @@ module EstablishTypeDefinitionCores =
25842589
hasMeasureAttr && not (match k with SynTypeDefnKind.Class | SynTypeDefnKind.Abbrev | SynTypeDefnKind.Opaque -> true | _ -> false) ||
25852590
hasInterfaceAttr && not (match k with SynTypeDefnKind.Interface -> true | _ -> false) ||
25862591
hasStructAttr && not (match k with SynTypeDefnKind.Struct | SynTypeDefnKind.Record | SynTypeDefnKind.Union -> true | _ -> false) then
2587-
error(Error(FSComp.SR.tcKindOfTypeSpecifiedDoesNotMatchDefinition(), m))
2592+
errorR(Error(FSComp.SR.tcKindOfTypeSpecifiedDoesNotMatchDefinition(), m))
25882593
k
25892594

25902595
[<return: Struct>]
@@ -2889,7 +2894,7 @@ module EstablishTypeDefinitionCores =
28892894

28902895
if hasMeasureAttr then
28912896
tycon.SetTypeOrMeasureKind TyparKind.Measure
2892-
if not (isNil typars) then error(Error(FSComp.SR.tcMeasureDefinitionsCannotHaveTypeParameters(), m))
2897+
if not (isNil typars) then errorR(Error(FSComp.SR.tcMeasureDefinitionsCannotHaveTypeParameters(), m))
28932898

28942899
let repr =
28952900
match synTyconRepr with
@@ -5150,9 +5155,9 @@ let CheckLetOrDoInNamespace binds m =
51505155
| [ SynBinding (accessibility=None; kind=(SynBindingKind.StandaloneExpression | SynBindingKind.Do); isInline=false; isMutable=false; attributes=[]; returnInfo=None; expr=(SynExpr.Do (expr=SynExpr.Const (constant=SynConst.Unit)) | SynExpr.Const (constant=SynConst.Unit))) ] ->
51515156
()
51525157
| [] ->
5153-
error(Error(FSComp.SR.tcNamespaceCannotContainValues(), m))
5158+
errorR(Error(FSComp.SR.tcNamespaceCannotContainValues(), m))
51545159
| _ ->
5155-
error(Error(FSComp.SR.tcNamespaceCannotContainValues(), binds.Head.RangeOfHeadPattern))
5160+
errorR(Error(FSComp.SR.tcNamespaceCannotContainValues(), binds.Head.RangeOfHeadPattern))
51565161

51575162
let rec TcMutRecDefsFinish cenv defs m =
51585163
let opens =

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ let TcUnionCaseOrExnField (cenv: cenv) (env: TcEnv) ty1 m longId fieldNum funcs
20372037
| _ -> error(Error(FSComp.SR.tcUnknownUnion(), m))
20382038

20392039
if fieldNum >= argTys.Length then
2040-
error (UnionCaseWrongNumberOfArgs(env.DisplayEnv, argTys.Length, fieldNum, m))
2040+
errorR (UnionCaseWrongNumberOfArgs(env.DisplayEnv, argTys.Length, fieldNum, m))
20412041

20422042
let ty2 = List.item fieldNum argTys
20432043
mkf, ty2

tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ type InterruptibleLazy<'T> private (valueFactory: unit -> 'T) =
842842
(Error 823, Line 10, Col 7, Line 10, Col 9, "The 'VolatileField' attribute may only be used on 'let' bindings in classes")
843843
(Error 879, Line 10, Col 7, Line 10, Col 9, "Volatile fields must be marked 'mutable' and cannot be thread-static")
844844
(Error 823, Line 26, Col 17, Line 26, Col 18, "The 'VolatileField' attribute may only be used on 'let' bindings in classes")
845+
(Error 880, Line 26, Col 17, Line 26, Col 18, "Uninitialized 'val' fields must be mutable and marked with the '[<DefaultValue>]' attribute. Consider using a 'let' binding instead of a 'val' field.")
845846
(Error 879, Line 14, Col 16, Line 14, Col 19, "Volatile fields must be marked 'mutable' and cannot be thread-static")
846847
(Error 879, Line 17, Col 16, Line 17, Col 17, "Volatile fields must be marked 'mutable' and cannot be thread-static")
847848
(Error 879, Line 20, Col 9, Line 20, Col 11, "Volatile fields must be marked 'mutable' and cannot be thread-static")
@@ -862,6 +863,7 @@ type InterruptibleLazy<'T> private (valueFactory: unit -> 'T) =
862863
(Error 823, Line 10, Col 7, Line 10, Col 9, "The 'VolatileField' attribute may only be used on 'let' bindings in classes")
863864
(Error 879, Line 10, Col 7, Line 10, Col 9, "Volatile fields must be marked 'mutable' and cannot be thread-static")
864865
(Error 823, Line 26, Col 17, Line 26, Col 18, "The 'VolatileField' attribute may only be used on 'let' bindings in classes")
866+
(Error 880, Line 26, Col 17, Line 26, Col 18, "Uninitialized 'val' fields must be mutable and marked with the '[<DefaultValue>]' attribute. Consider using a 'let' binding instead of a 'val' field.")
865867
(Error 879, Line 14, Col 16, Line 14, Col 19, "Volatile fields must be marked 'mutable' and cannot be thread-static")
866868
(Error 879, Line 17, Col 16, Line 17, Col 17, "Volatile fields must be marked 'mutable' and cannot be thread-static")
867869
(Error 879, Line 20, Col 9, Line 20, Col 11, "Volatile fields must be marked 'mutable' and cannot be thread-static")

tests/FSharp.Compiler.ComponentTests/Conformance/UnitsOfMeasure/WithOOP/WithOOP.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module WithOOP =
5757
|> getCompilation
5858
|> shouldFailWithDiagnostics [
5959
(Error 904, Line 8, Col 5, Line 8, Col 18, "Measure declarations may have only static members: constructors are not available")
60+
(Error 904, Line 9, Col 5, Line 9, Col 25, "Measure declarations may have only static members: constructors are not available")
6061
]
6162

6263
[<Theory; FileInlineData("E_NoInstanceOnMeasure01.fsx")>]

tests/fsharp/typecheck/sigs/neg10.bsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ neg10.fs(84,13,84,29): typecheck error FS0896: Enumerations cannot have members
3737

3838
neg10.fs(90,23,90,41): typecheck error FS0907: Enumerations cannot have interface declarations
3939

40+
neg10.fs(91,17,91,45): typecheck error FS0896: Enumerations cannot have members
41+
4042
neg10.fs(99,23,99,29): typecheck error FS0907: Enumerations cannot have interface declarations
4143

4244
neg10.fs(107,10,107,17): typecheck error FS0964: Type abbreviations cannot have augmentations
@@ -51,6 +53,8 @@ neg10.fs(120,10,120,17): typecheck error FS0964: Type abbreviations cannot have
5153

5254
neg10.fs(122,23,122,41): typecheck error FS0906: Type abbreviations cannot have interface declarations
5355

56+
neg10.fs(123,17,123,45): typecheck error FS0895: Type abbreviations cannot have members
57+
5458
neg10.fs(129,10,129,17): typecheck error FS0964: Type abbreviations cannot have augmentations
5559

5660
neg10.fs(131,23,131,29): typecheck error FS0906: Type abbreviations cannot have interface declarations

0 commit comments

Comments
 (0)