Skip to content

Commit 234ce6d

Browse files
authored
Checker: fix declaring type for abbreviated types extensions (#18909)
1 parent 04ebcb7 commit 234ce6d

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation. ([Issue ](https://github.com/dotnet/fsharp/pull/18851))
2929
* Format top-level generic types using a prefix style in inherit/interface declarations and flexible type annotations. ([PR #18897](https://github.com/dotnet/fsharp/pull/18897))
3030
* Parser: fix range for computed binding expressions ([PR #18903](https://github.com/dotnet/fsharp/pull/18903))
31+
* Checker: fix declaring type for abbreviated types extensions ([PR #18909](https://github.com/dotnet/fsharp/pull/18909))
3132

3233
### Changed
3334
* Use `errorR` instead of `error` in `CheckDeclarations.fs` when possible. ([PR #18645](https://github.com/dotnet/fsharp/pull/18645))

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,11 +4250,9 @@ module TcDeclarations =
42504250
// a) For interfaces, only if it is in the original defn.
42514251
// Augmentations to interfaces via partial type defns will always be extensions, e.g. extension members on interfaces.
42524252
// b) For other types, if the type is isInSameModuleOrNamespace
4253-
let declKind, typars =
4254-
if isAtOriginalTyconDefn then
4255-
ModuleOrMemberBinding, reqTypars
4256-
4257-
else
4253+
if isAtOriginalTyconDefn then
4254+
ModuleOrMemberBinding, tcref, reqTypars
4255+
else
42584256
let isInSameModuleOrNamespace =
42594257
match envForDecls.eModuleOrNamespaceTypeAccumulator.Value.TypesByMangledName.TryGetValue tcref.LogicalName with
42604258
| true, tycon -> tyconOrder.Compare(tcref.Deref, tycon) = 0
@@ -4270,25 +4268,24 @@ module TcDeclarations =
42704268
let _tpenv = TcTyparConstraints cenv NoNewTypars CheckCxs ItemOccurrence.UseInType envForTycon emptyUnscopedTyparEnv synTyparCxs
42714269
declaredTypars |> List.iter (SetTyparRigid envForDecls.DisplayEnv m)
42724270

4273-
if isInSameModuleOrNamespace && not isInterfaceOrDelegateOrEnum then
4271+
if tcref.TypeAbbrev.IsSome then
4272+
ExtrinsicExtensionBinding, tcref, declaredTypars
4273+
elif isInSameModuleOrNamespace && not isInterfaceOrDelegateOrEnum then
42744274
// For historical reasons we only give a warning for incorrect type parameters on intrinsic extensions
42754275
if nReqTypars <> synTypars.Length then
42764276
errorR(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m))
42774277
if not (typarsAEquiv g (TypeEquivEnv.EmptyWithNullChecks g) reqTypars declaredTypars) then
42784278
warning(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m))
42794279
// Note we return 'reqTypars' for intrinsic extensions since we may only have given warnings
4280-
IntrinsicExtensionBinding, reqTypars
4280+
IntrinsicExtensionBinding, tcref, reqTypars
42814281
else
42824282
if isInSameModuleOrNamespace && isDelegateOrEnum then
42834283
errorR(Error(FSComp.SR.tcMembersThatExtendInterfaceMustBePlacedInSeparateModule(), tcref.Range))
42844284
if nReqTypars <> synTypars.Length then
42854285
error(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m))
42864286
if not (typarsAEquiv g (TypeEquivEnv.EmptyWithNullChecks g) reqTypars declaredTypars) then
42874287
errorR(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m))
4288-
ExtrinsicExtensionBinding, declaredTypars
4289-
4290-
4291-
declKind, tcref, typars
4288+
ExtrinsicExtensionBinding, tcref, declaredTypars
42924289

42934290

42944291
let private isAugmentationTyconDefnRepr = function SynTypeDefnSimpleRepr.General(kind=SynTypeDefnKind.Augmentation _) -> true | _ -> false

tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ type FloatAlias with
5454
(Error 909, Line 6, Col 15, Line 6, Col 34, "All implemented interfaces should be declared on the initial declaration of the type")
5555
]
5656

57+
[<Fact>]
58+
let ``Members 04 - Error in expr`` () =
59+
Fsx """
60+
type StringAlias = string
61+
62+
type StringAlias with
63+
member x.Length = x.Length + ""
64+
"""
65+
|> typecheck
66+
|> shouldFail
67+
|> withDiagnostics [
68+
(Error 964, Line 4, Col 6, Line 4, Col 17, "Type abbreviations cannot have augmentations")
69+
(Error 895, Line 5, Col 5, Line 5, Col 36, "Type abbreviations cannot have members")
70+
(Error 1, Line 5, Col 34, Line 5, Col 36, "The type 'string' does not match the type 'int'")
71+
(Error 43, Line 5, Col 32, Line 5, Col 33, "The type 'string' does not match the type 'int'")
72+
]
73+
5774
[<Fact>]
5875
let ``Multiple types 01`` () =
5976
Fsx """

0 commit comments

Comments
 (0)