diff --git a/docs/release-notes/.FSharp.Compiler.Service/10.0.100.md b/docs/release-notes/.FSharp.Compiler.Service/10.0.100.md index 6ea4c6a7944..2848b46c6eb 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/10.0.100.md +++ b/docs/release-notes/.FSharp.Compiler.Service/10.0.100.md @@ -1,5 +1,6 @@ ### Added * Add opt-in warning attribute not valid for union case with fields [PR #18532](https://github.com/dotnet/fsharp/pull/18532)) +* Add support for `when 'T : Enum` library-only static optimization constraint. ([PR #18546](https://github.com/dotnet/fsharp/pull/18546)) ### Fixed @@ -12,9 +13,8 @@ * Completion: fix previous namespace considered opened [PR #18609](https://github.com/dotnet/fsharp/pull/18609) * Fix active pattern typechecking regression. ([Issue #18638](https://github.com/dotnet/fsharp/issues/18638), [PR #18642](https://github.com/dotnet/fsharp/pull/18642)) -### Added - -* Add support for `when 'T : Enum` library-only library-only static optimization constraint. ([PR #18546](https://github.com/dotnet/fsharp/pull/18546)) +### Changed +* Use `errorR` instead of `error` in `CheckDeclarations.fs` when possible. ([PR #18645](https://github.com/dotnet/fsharp/pull/18645)) ### Breaking Changes diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index dd0825f4bb5..adcd4d80f30 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -441,10 +441,10 @@ module TcRecdUnionAndEnumDeclarations = let isThreadStatic = isThreadOrContextStatic g attrsForField if isThreadStatic && (not zeroInit || not isStatic) then - error(Error(FSComp.SR.tcThreadStaticAndContextStaticMustBeStatic(), m)) + errorR(Error(FSComp.SR.tcThreadStaticAndContextStaticMustBeStatic(), m)) if isVolatile then - error(Error(FSComp.SR.tcVolatileOnlyOnClassLetBindings(), m)) + errorR(Error(FSComp.SR.tcVolatileOnlyOnClassLetBindings(), m)) if isIncrClass && (not zeroInit || not isMutable) then errorR(Error(FSComp.SR.tcUninitializedValFieldsMustBeMutable(), m)) let isPrivate = match vis with | Some (SynAccess.Private _) -> true | _ -> false @@ -566,7 +566,7 @@ module TcRecdUnionAndEnumDeclarations = MakeRecdFieldSpec g env parent (false, None, argTy, [], [], id, argInfo.Name.IsNone, false, false, XmlDoc.Empty, None, m)) if not (typeEquiv g recordTy thisTy) then - error(Error(FSComp.SR.tcReturnTypesForUnionMustBeSameAsType(), m)) + errorR(Error(FSComp.SR.tcReturnTypesForUnionMustBeSameAsType(), m)) rfields, recordTy let names = rfields @@ -756,10 +756,10 @@ let TcOpenTypeDecl (cenv: cenv) mOpenDecl scopem env (synType: SynType, m) = let ty, _tpenv = TcType cenv NoNewTypars CheckCxs ItemOccurrence.Open WarnOnIWSAM.Yes env emptyUnscopedTyparEnv synType if not (isAppTy g ty) then - error(Error(FSComp.SR.tcNamedTypeRequired("open type"), m)) + errorR(Error(FSComp.SR.tcNamedTypeRequired("open type"), m)) if isByrefTy g ty then - error(Error(FSComp.SR.tcIllegalByrefsInOpenTypeDeclaration(), m)) + errorR(Error(FSComp.SR.tcIllegalByrefsInOpenTypeDeclaration(), m)) let openDecl = OpenDeclaration.Create (SynOpenDeclTarget.Type (synType, m), [], [ty], scopem, false) let env = OpenTypeContent cenv.tcSink g cenv.amap scopem env ty openDecl @@ -1065,11 +1065,11 @@ module MutRecBindingChecking = if tcref.IsTypeAbbrev then // ideally we'd have the 'm' of the type declaration stored here, to avoid needing to trim to line to approx - error(Error(FSComp.SR.tcTypeAbbreviationsMayNotHaveMembers(), (trimRangeToLine m))) + errorR(Error(FSComp.SR.tcTypeAbbreviationsMayNotHaveMembers(), (trimRangeToLine m))) if tcref.IsEnumTycon && (declKind <> ExtrinsicExtensionBinding) && classMemberDef.IsSome then // ideally we'd have the 'm' of the type declaration stored here, to avoid needing to trim to line to approx - error(Error(FSComp.SR.tcEnumerationsMayNotHaveMembers(), (trimRangeToLine m))) + errorR(Error(FSComp.SR.tcEnumerationsMayNotHaveMembers(), (trimRangeToLine m))) match classMemberDef, containerInfo with @@ -1086,7 +1086,7 @@ module MutRecBindingChecking = let (MemberOrValContainerInfo(tcref, _, baseValOpt, safeInitInfo, _)) = memberContainerInfo if tcref.TypeOrMeasureKind = TyparKind.Measure then - error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) + errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) // Phase2A: make staticCtorInfo - ctorv, thisVal etc, type depends on argty(s) let staticCtorInfo = TcStaticImplicitCtorInfo_Phase2A(cenv, envForTycon, tcref, m, copyOfTyconTypars) @@ -1102,7 +1102,7 @@ module MutRecBindingChecking = | Some (SynMemberDefn.ImplicitInherit (ty, arg, _baseIdOpt, m, _)), _ -> if tcref.TypeOrMeasureKind = TyparKind.Measure then - error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) + errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) // Phase2A: inherit ty(arg) as base - pass through // Phase2A: pick up baseValOpt! @@ -1112,7 +1112,7 @@ module MutRecBindingChecking = | Some (SynMemberDefn.LetBindings (letBinds, isStatic, isRec, m)), _ -> match tcref.TypeOrMeasureKind, isStatic with - | TyparKind.Measure, false -> error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) + | TyparKind.Measure, false -> errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) | _ -> () if not isStatic && tcref.IsStructOrEnumTycon then @@ -1149,9 +1149,9 @@ module MutRecBindingChecking = match memberFlagsOpt with | None -> () | Some memberFlags -> - if memberFlags.IsInstance then error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) + if memberFlags.IsInstance then errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembers(), m)) match memberFlags.MemberKind with - | SynMemberKind.Constructor -> error(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembersNotConstructors(), m)) + | SynMemberKind.Constructor -> errorR(Error(FSComp.SR.tcMeasureDeclarationsRequireStaticMembersNotConstructors(), m)) | _ -> () let envForMember = @@ -1880,7 +1880,7 @@ module MutRecBindingChecking = for extraTypar in allExtraGeneralizableTypars do if Zset.memberOf freeInInitialEnv extraTypar then let ty = mkTyparTy extraTypar - error(Error(FSComp.SR.tcNotSufficientlyGenericBecauseOfScope(NicePrint.prettyStringOfTy denv ty), extraTypar.Range)) + errorR(Error(FSComp.SR.tcNotSufficientlyGenericBecauseOfScope(NicePrint.prettyStringOfTy denv ty), extraTypar.Range)) // Solve any type variables in any part of the overall type signature of the class whose // constraints involve generalized type variables. @@ -1980,8 +1980,8 @@ let TcMutRecDefns_Phase2 (cenv: cenv) envInitial mBinds scopem mutRecNSInfo (env | SynMemberDefn.Interface(interfaceType=intfTy; members=defnOpt) -> let ty = if tcref.Deref.IsFSharpException then g.exn_ty else generalizedTyconRef g tcref let m = intfTy.Range - if tcref.IsTypeAbbrev then error(Error(FSComp.SR.tcTypeAbbreviationsCannotHaveInterfaceDeclaration(), m)) - if tcref.IsEnumTycon then error(Error(FSComp.SR.tcEnumerationsCannotHaveInterfaceDeclaration(), m)) + if tcref.IsTypeAbbrev then errorR(Error(FSComp.SR.tcTypeAbbreviationsCannotHaveInterfaceDeclaration(), m)) + if tcref.IsEnumTycon then errorR(Error(FSComp.SR.tcEnumerationsCannotHaveInterfaceDeclaration(), m)) let intfTyR = let envinner = AddDeclaredTypars CheckForDuplicateTypars declaredTyconTypars envForTycon @@ -2053,7 +2053,7 @@ let TcMutRecDefns_Phase2 (cenv: cenv) envInitial mBinds scopem mutRecNSInfo (env | SynMemberDefn.AbstractSlot _ | SynMemberDefn.ValField _ | SynMemberDefn.Inherit _ -> error(InternalError("Unexpected declaration element", memb.Range)) - | SynMemberDefn.NestedType _ -> error(Error(FSComp.SR.tcTypesCannotContainNestedTypes(), memb.Range)) ] + | SynMemberDefn.NestedType _ -> errorR(Error(FSComp.SR.tcTypesCannotContainNestedTypes(), memb.Range)) ] let tpenv = emptyUnscopedTyparEnv @@ -2381,7 +2381,7 @@ module TyconConstraintInference = //------------------------------------------------------------------------- let ComputeModuleName (longPath: Ident list) = - if longPath.Length <> 1 then error(Error(FSComp.SR.tcInvalidModuleName(), (List.head longPath).idRange)) + if longPath.Length <> 1 then errorR(Error(FSComp.SR.tcInvalidModuleName(), (List.head longPath).idRange)) longPath.Head let CheckForDuplicateConcreteType env nm m = @@ -2418,7 +2418,12 @@ module TcExceptionDeclarations = let TcExnDefnCore_Phase1G_EstablishRepresentation (cenv: cenv) (env: TcEnv) parent (exnc: Entity) (SynExceptionDefnRepr(_, SynUnionCase(caseType=args), reprIdOpt, _, _, m)) = let g = cenv.g - let args = match args with SynUnionCaseKind.Fields args -> args | _ -> error(Error(FSComp.SR.tcExplicitTypeSpecificationCannotBeUsedForExceptionConstructors(), m)) + let args = + match args with + | SynUnionCaseKind.Fields args -> args + | _ -> + errorR(Error(FSComp.SR.tcExplicitTypeSpecificationCannotBeUsedForExceptionConstructors(), m)) + [] let ad = env.AccessRights let id = exnc.Id @@ -2450,7 +2455,7 @@ module TcExceptionDeclarations = // REVIEW: check this really is an exception type match args' with | [] -> () - | _ -> error (Error(FSComp.SR.tcAbbreviationsFordotNetExceptionsCannotTakeArguments(), m)) + | _ -> errorR (Error(FSComp.SR.tcAbbreviationsFordotNetExceptionsCannotTakeArguments(), m)) let candidates = meths |> List.filter (fun minfo -> minfo.NumArgs = [args'.Length] && @@ -2545,7 +2550,7 @@ module EstablishTypeDefinitionCores = /// Compute the mangled name of a type definition. 'doErase' is true for all type definitions except type abbreviations. let private ComputeTyconName (longPath: Ident list, doErase: bool, typars: Typars) = - if longPath.Length <> 1 then error(Error(FSComp.SR.tcInvalidTypeExtension(), longPath.Head.idRange)) + if longPath.Length <> 1 then errorR(Error(FSComp.SR.tcInvalidTypeExtension(), longPath.Head.idRange)) let id = longPath.Head let erasedArity = if doErase then typars |> Seq.sumBy (fun tp -> if tp.IsErased then 0 else 1) @@ -2569,7 +2574,7 @@ module EstablishTypeDefinitionCores = let bi b = (if b then 1 else 0) if (bi hasClassAttr + bi hasInterfaceAttr + bi hasStructAttr + bi hasMeasureAttr) > 1 || (bi hasAbstractClassAttr + bi hasInterfaceAttr + bi hasStructAttr + bi hasMeasureAttr) > 1 then - error(Error(FSComp.SR.tcAttributesOfTypeSpecifyMultipleKindsForType(), m)) + errorR(Error(FSComp.SR.tcAttributesOfTypeSpecifyMultipleKindsForType(), m)) match kind with | SynTypeDefnKind.Unspecified -> @@ -2584,7 +2589,7 @@ module EstablishTypeDefinitionCores = hasMeasureAttr && not (match k with SynTypeDefnKind.Class | SynTypeDefnKind.Abbrev | SynTypeDefnKind.Opaque -> true | _ -> false) || hasInterfaceAttr && not (match k with SynTypeDefnKind.Interface -> true | _ -> false) || hasStructAttr && not (match k with SynTypeDefnKind.Struct | SynTypeDefnKind.Record | SynTypeDefnKind.Union -> true | _ -> false) then - error(Error(FSComp.SR.tcKindOfTypeSpecifiedDoesNotMatchDefinition(), m)) + errorR(Error(FSComp.SR.tcKindOfTypeSpecifiedDoesNotMatchDefinition(), m)) k [] @@ -2889,7 +2894,7 @@ module EstablishTypeDefinitionCores = if hasMeasureAttr then tycon.SetTypeOrMeasureKind TyparKind.Measure - if not (isNil typars) then error(Error(FSComp.SR.tcMeasureDefinitionsCannotHaveTypeParameters(), m)) + if not (isNil typars) then errorR(Error(FSComp.SR.tcMeasureDefinitionsCannotHaveTypeParameters(), m)) let repr = match synTyconRepr with @@ -5150,9 +5155,9 @@ let CheckLetOrDoInNamespace binds m = | [ 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))) ] -> () | [] -> - error(Error(FSComp.SR.tcNamespaceCannotContainValues(), m)) + errorR(Error(FSComp.SR.tcNamespaceCannotContainValues(), m)) | _ -> - error(Error(FSComp.SR.tcNamespaceCannotContainValues(), binds.Head.RangeOfHeadPattern)) + errorR(Error(FSComp.SR.tcNamespaceCannotContainValues(), binds.Head.RangeOfHeadPattern)) let rec TcMutRecDefsFinish cenv defs m = let opens = diff --git a/src/Compiler/Checking/Expressions/CheckExpressions.fs b/src/Compiler/Checking/Expressions/CheckExpressions.fs index e1410e8c595..bd48bf4de50 100644 --- a/src/Compiler/Checking/Expressions/CheckExpressions.fs +++ b/src/Compiler/Checking/Expressions/CheckExpressions.fs @@ -2037,7 +2037,7 @@ let TcUnionCaseOrExnField (cenv: cenv) (env: TcEnv) ty1 m longId fieldNum funcs | _ -> error(Error(FSComp.SR.tcUnknownUnion(), m)) if fieldNum >= argTys.Length then - error (UnionCaseWrongNumberOfArgs(env.DisplayEnv, argTys.Length, fieldNum, m)) + errorR (UnionCaseWrongNumberOfArgs(env.DisplayEnv, argTys.Length, fieldNum, m)) let ty2 = List.item fieldNum argTys mkf, ty2 diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs index b0fa891d2b7..a2ee105f91c 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs @@ -842,6 +842,7 @@ type InterruptibleLazy<'T> private (valueFactory: unit -> 'T) = (Error 823, Line 10, Col 7, Line 10, Col 9, "The 'VolatileField' attribute may only be used on 'let' bindings in classes") (Error 879, Line 10, Col 7, Line 10, Col 9, "Volatile fields must be marked 'mutable' and cannot be thread-static") (Error 823, Line 26, Col 17, Line 26, Col 18, "The 'VolatileField' attribute may only be used on 'let' bindings in classes") + (Error 880, Line 26, Col 17, Line 26, Col 18, "Uninitialized 'val' fields must be mutable and marked with the '[]' attribute. Consider using a 'let' binding instead of a 'val' field.") (Error 879, Line 14, Col 16, Line 14, Col 19, "Volatile fields must be marked 'mutable' and cannot be thread-static") (Error 879, Line 17, Col 16, Line 17, Col 17, "Volatile fields must be marked 'mutable' and cannot be thread-static") (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) = (Error 823, Line 10, Col 7, Line 10, Col 9, "The 'VolatileField' attribute may only be used on 'let' bindings in classes") (Error 879, Line 10, Col 7, Line 10, Col 9, "Volatile fields must be marked 'mutable' and cannot be thread-static") (Error 823, Line 26, Col 17, Line 26, Col 18, "The 'VolatileField' attribute may only be used on 'let' bindings in classes") + (Error 880, Line 26, Col 17, Line 26, Col 18, "Uninitialized 'val' fields must be mutable and marked with the '[]' attribute. Consider using a 'let' binding instead of a 'val' field.") (Error 879, Line 14, Col 16, Line 14, Col 19, "Volatile fields must be marked 'mutable' and cannot be thread-static") (Error 879, Line 17, Col 16, Line 17, Col 17, "Volatile fields must be marked 'mutable' and cannot be thread-static") (Error 879, Line 20, Col 9, Line 20, Col 11, "Volatile fields must be marked 'mutable' and cannot be thread-static") diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/UnitsOfMeasure/WithOOP/WithOOP.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/UnitsOfMeasure/WithOOP/WithOOP.fs index b915fe62341..d6fa3b02810 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/UnitsOfMeasure/WithOOP/WithOOP.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/UnitsOfMeasure/WithOOP/WithOOP.fs @@ -57,6 +57,7 @@ module WithOOP = |> getCompilation |> shouldFailWithDiagnostics [ (Error 904, Line 8, Col 5, Line 8, Col 18, "Measure declarations may have only static members: constructors are not available") + (Error 904, Line 9, Col 5, Line 9, Col 25, "Measure declarations may have only static members: constructors are not available") ] [] diff --git a/tests/fsharp/typecheck/sigs/neg10.bsl b/tests/fsharp/typecheck/sigs/neg10.bsl index 31bcdb1882e..64fcc4eefbc 100644 --- a/tests/fsharp/typecheck/sigs/neg10.bsl +++ b/tests/fsharp/typecheck/sigs/neg10.bsl @@ -37,6 +37,8 @@ neg10.fs(84,13,84,29): typecheck error FS0896: Enumerations cannot have members neg10.fs(90,23,90,41): typecheck error FS0907: Enumerations cannot have interface declarations +neg10.fs(91,17,91,45): typecheck error FS0896: Enumerations cannot have members + neg10.fs(99,23,99,29): typecheck error FS0907: Enumerations cannot have interface declarations 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 neg10.fs(122,23,122,41): typecheck error FS0906: Type abbreviations cannot have interface declarations +neg10.fs(123,17,123,45): typecheck error FS0895: Type abbreviations cannot have members + neg10.fs(129,10,129,17): typecheck error FS0964: Type abbreviations cannot have augmentations neg10.fs(131,23,131,29): typecheck error FS0906: Type abbreviations cannot have interface declarations