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 c87819d01ba..1419988c5e6 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/10.0.100.md +++ b/docs/release-notes/.FSharp.Compiler.Service/10.0.100.md @@ -26,6 +26,7 @@ * Fix nullable types formatting in `FSharpType.Format` and tooltips to include parentheses. ([PR #18842](https://github.com/dotnet/fsharp/pull/18842)) * TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation. ([Issue ](https://github.com/dotnet/fsharp/pull/18851)) * Parser: fix range for computed binding expressions ([PR #18903](https://github.com/dotnet/fsharp/pull/18903)) +* Checker: fix declaring type for abbreviated types extensions ([PR #18909](https://github.com/dotnet/fsharp/pull/18909)) ### Changed * Use `errorR` instead of `error` in `CheckDeclarations.fs` when possible. ([PR #18645](https://github.com/dotnet/fsharp/pull/18645)) diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index e6a39af0a7b..cbf79fbdfb3 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -4250,11 +4250,9 @@ module TcDeclarations = // a) For interfaces, only if it is in the original defn. // Augmentations to interfaces via partial type defns will always be extensions, e.g. extension members on interfaces. // b) For other types, if the type is isInSameModuleOrNamespace - let declKind, typars = - if isAtOriginalTyconDefn then - ModuleOrMemberBinding, reqTypars - - else + if isAtOriginalTyconDefn then + ModuleOrMemberBinding, tcref, reqTypars + else let isInSameModuleOrNamespace = match envForDecls.eModuleOrNamespaceTypeAccumulator.Value.TypesByMangledName.TryGetValue tcref.LogicalName with | true, tycon -> tyconOrder.Compare(tcref.Deref, tycon) = 0 @@ -4270,14 +4268,16 @@ module TcDeclarations = let _tpenv = TcTyparConstraints cenv NoNewTypars CheckCxs ItemOccurrence.UseInType envForTycon emptyUnscopedTyparEnv synTyparCxs declaredTypars |> List.iter (SetTyparRigid envForDecls.DisplayEnv m) - if isInSameModuleOrNamespace && not isInterfaceOrDelegateOrEnum then + if tcref.TypeAbbrev.IsSome then + ExtrinsicExtensionBinding, tcref, declaredTypars + elif isInSameModuleOrNamespace && not isInterfaceOrDelegateOrEnum then // For historical reasons we only give a warning for incorrect type parameters on intrinsic extensions if nReqTypars <> synTypars.Length then errorR(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m)) if not (typarsAEquiv g (TypeEquivEnv.EmptyWithNullChecks g) reqTypars declaredTypars) then warning(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m)) // Note we return 'reqTypars' for intrinsic extensions since we may only have given warnings - IntrinsicExtensionBinding, reqTypars + IntrinsicExtensionBinding, tcref, reqTypars else if isInSameModuleOrNamespace && isDelegateOrEnum then errorR(Error(FSComp.SR.tcMembersThatExtendInterfaceMustBePlacedInSeparateModule(), tcref.Range)) @@ -4285,10 +4285,7 @@ module TcDeclarations = error(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m)) if not (typarsAEquiv g (TypeEquivEnv.EmptyWithNullChecks g) reqTypars declaredTypars) then errorR(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m)) - ExtrinsicExtensionBinding, declaredTypars - - - declKind, tcref, typars + ExtrinsicExtensionBinding, tcref, declaredTypars let private isAugmentationTyconDefnRepr = function SynTypeDefnSimpleRepr.General(kind=SynTypeDefnKind.Augmentation _) -> true | _ -> false diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs index 26966e72782..41d7a86677a 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs @@ -54,6 +54,23 @@ type FloatAlias with (Error 909, Line 6, Col 15, Line 6, Col 34, "All implemented interfaces should be declared on the initial declaration of the type") ] +[] +let ``Members 04 - Error in expr`` () = + Fsx """ +type StringAlias = string + +type StringAlias with + member x.Length = x.Length + "" + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 17, "Type abbreviations cannot have augmentations") + (Error 895, Line 5, Col 5, Line 5, Col 36, "Type abbreviations cannot have members") + (Error 1, Line 5, Col 34, Line 5, Col 36, "The type 'string' does not match the type 'int'") + (Error 43, Line 5, Col 32, Line 5, Col 33, "The type 'string' does not match the type 'int'") + ] + [] let ``Multiple types 01`` () = Fsx """