Skip to content

Commit 346a59f

Browse files
authored
Better error range for struct and restricted super types inherit (#17886)
* Better error range for struct inherit * CheckSuperType * release notes * E_RestrictedSuperTypes * neg6.bsl * neg4.bsl
1 parent a89514e commit 346a59f

File tree

10 files changed

+74
-30
lines changed

10 files changed

+74
-30
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
* Better ranges for CE `match!`. ([PR #17789](https://github.com/dotnet/fsharp/pull/17789))
2222
* Better ranges for CE `use` error reporting. ([PR #17811](https://github.com/dotnet/fsharp/pull/17811))
2323
* Better ranges for `inherit` error reporting. ([PR #17879](https://github.com/dotnet/fsharp/pull/17879))
24+
* Better ranges for `inherit` `struct` error reporting. ([PR #17886](https://github.com/dotnet/fsharp/pull/17886))
2425

2526
### Breaking Changes

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3322,7 +3322,7 @@ module EstablishTypeDefinitionCores =
33223322
| SynTypeDefnSimpleRepr.Record _ ->
33233323
if tycon.IsStructRecordOrUnionTycon then Some(g.system_Value_ty)
33243324
else None
3325-
| SynTypeDefnSimpleRepr.General (kind, _, slotsigs, fields, isConcrete, _, _, _) ->
3325+
| SynTypeDefnSimpleRepr.General (kind, inherits, slotsigs, fields, isConcrete, _, _, _) ->
33263326
let kind = InferTyconKind g (kind, attrs, slotsigs, fields, inSig, isConcrete, m)
33273327

33283328
match inheritedTys with
@@ -3333,15 +3333,20 @@ module EstablishTypeDefinitionCores =
33333333
| SynTypeDefnKind.Opaque | SynTypeDefnKind.Class | SynTypeDefnKind.Interface -> None
33343334
| _ -> error(InternalError("should have inferred tycon kind", m))
33353335

3336-
| [(ty, m)] ->
3337-
if not firstPass && not (match kind with SynTypeDefnKind.Class -> true | _ -> false) then
3338-
errorR (Error(FSComp.SR.tcStructsInterfacesEnumsDelegatesMayNotInheritFromOtherTypes(), m))
3339-
CheckSuperType cenv ty m
3336+
| [(ty, m)] ->
3337+
let inheritRange =
3338+
match inherits with
3339+
| [] -> m
3340+
| (synType, _, _) :: _ -> synType.Range
3341+
if not firstPass && not (match kind with SynTypeDefnKind.Class -> true | _ -> false) then
3342+
errorR (Error(FSComp.SR.tcStructsInterfacesEnumsDelegatesMayNotInheritFromOtherTypes(), inheritRange))
3343+
CheckSuperType cenv ty inheritRange
33403344
if isTyparTy g ty then
33413345
if firstPass then
3342-
errorR(Error(FSComp.SR.tcCannotInheritFromVariableType(), m))
3346+
errorR(Error(FSComp.SR.tcCannotInheritFromVariableType(), inheritRange))
33433347
Some g.obj_ty_noNulls // a "super" that is a variable type causes grief later
3344-
else
3348+
else
3349+
33453350
Some ty
33463351
| _ ->
33473352
error(Error(FSComp.SR.tcTypesCannotInheritFromMultipleConcreteTypes(), m))

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7140,7 +7140,7 @@ and CheckSuperType (cenv: cenv) ty m =
71407140
typeEquiv g ty g.system_Array_ty ||
71417141
typeEquiv g ty g.system_MulticastDelegate_ty ||
71427142
typeEquiv g ty g.system_Delegate_ty then
7143-
error(Error(FSComp.SR.tcPredefinedTypeCannotBeUsedAsSuperType(), m))
7143+
errorR(Error(FSComp.SR.tcPredefinedTypeCannotBeUsedAsSuperType(), m))
71447144

71457145
if isErasedType g ty then
71467146
errorR(Error(FSComp.SR.tcCannotInheritFromErasedType(), m))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type TK_I_005 =
2+
abstract M : unit -> unit
3+
4+
[<Struct>]
5+
type TK_I_006b =
6+
inherit TK_I_005
7+

tests/FSharp.Compiler.ComponentTests/Conformance/Types/StructTypes/StructTypes.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ module StructTypes =
3838
|> shouldSucceed
3939
|> ignore
4040

41+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_Inheritance.fs"|])>]
42+
let ``StructTypes - E_Inheritance.fs`` compilation =
43+
compilation
44+
|> asFsx
45+
|> withOptions ["--warnaserror+"; "--nowarn:988"]
46+
|> typecheck
47+
|> shouldFail
48+
|> withDiagnostics [
49+
(Error 931, Line 6, Col 12, Line 6, Col 20, "Structs, interfaces, enums and delegates cannot inherit from other types");
50+
(Error 946, Line 6, Col 12, Line 6, Col 20, "Cannot inherit from interface type. Use interface ... with instead.")
51+
]
52+

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,23 @@ type Class() =
830830
|> withDiagnostics [
831831
(Error 961, Line 5, Col 5, Line 5, Col 12, "This 'inherit' declaration specifies the inherited type but no arguments. Consider supplying arguments, e.g. 'inherit BaseType(args)'.")
832832
(Error 946, Line 5, Col 13, Line 5, Col 15, "Cannot inherit from interface type. Use interface ... with instead.")
833+
]
834+
835+
[<Fact>]
836+
let ``The types System.ValueType, System.Enum, System.Delegate, System.MulticastDelegate and System.Array cannot be used as super types in an object expression or class.`` () =
837+
Fsx """
838+
type C1 = class inherit System.ValueType override x.ToString() = "" end
839+
type C2 = class inherit System.Array override x.ToString() = "" end
840+
type C3 = class inherit System.Enum override x.ToString() = "" end
841+
type C4 = class inherit System.Delegate override x.ToString() = "" end
842+
type C5 = class inherit System.MulticastDelegate override x.ToString() = "" end
843+
"""
844+
|> typecheck
845+
|> shouldFail
846+
|> withDiagnostics [
847+
(Error 771, Line 2, Col 25, Line 2, Col 41, "The types System.ValueType, System.Enum, System.Delegate, System.MulticastDelegate and System.Array cannot be used as super types in an object expression or class");
848+
(Error 771, Line 3, Col 25, Line 3, Col 37, "The types System.ValueType, System.Enum, System.Delegate, System.MulticastDelegate and System.Array cannot be used as super types in an object expression or class");
849+
(Error 771, Line 4, Col 25, Line 4, Col 36, "The types System.ValueType, System.Enum, System.Delegate, System.MulticastDelegate and System.Array cannot be used as super types in an object expression or class")
850+
(Error 771, Line 5, Col 25, Line 5, Col 40, "The types System.ValueType, System.Enum, System.Delegate, System.MulticastDelegate and System.Array cannot be used as super types in an object expression or class");
851+
(Error 771, Line 6, Col 25, Line 6, Col 49, "The types System.ValueType, System.Enum, System.Delegate, System.MulticastDelegate and System.Array cannot be used as super types in an object expression or class")
833852
]

tests/fsharp/typeProviders/negTests/neg4.bsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ neg4.fsx(30,5,30,22): typecheck error FS3062: This type test with a provided typ
1717

1818
neg4.fsx(36,5,36,28): typecheck error FS3060: This type test or downcast will erase the provided type 'HelloWorldType' to the type 'System.Object'
1919

20-
neg4.fsx(42,8,42,33): typecheck error FS3063: Cannot inherit from erased provided type
20+
neg4.fsx(42,16,42,33): typecheck error FS3063: Cannot inherit from erased provided type
2121

22-
neg4.fsx(42,8,42,33): typecheck error FS3063: Cannot inherit from erased provided type
22+
neg4.fsx(42,16,42,33): typecheck error FS3063: Cannot inherit from erased provided type
2323

24-
neg4.fsx(47,8,47,35): typecheck error FS3063: Cannot inherit from erased provided type
24+
neg4.fsx(47,16,47,33): typecheck error FS3063: Cannot inherit from erased provided type
2525

26-
neg4.fsx(47,8,47,35): typecheck error FS3063: Cannot inherit from erased provided type
26+
neg4.fsx(47,16,47,33): typecheck error FS3063: Cannot inherit from erased provided type
2727

2828
neg4.fsx(52,25,52,42): typecheck error FS3063: Cannot inherit from erased provided type
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
neg6.fsx(8,3,8,27): typecheck error FS3063: Cannot inherit from erased provided type
2+
neg6.fsx(8,11,8,25): typecheck error FS3063: Cannot inherit from erased provided type
33

4-
neg6.fsx(8,3,8,27): typecheck error FS3063: Cannot inherit from erased provided type
4+
neg6.fsx(8,11,8,25): typecheck error FS3063: Cannot inherit from erased provided type
55

6-
neg6.fsx(11,3,11,30): typecheck error FS3063: Cannot inherit from erased provided type
6+
neg6.fsx(11,11,11,28): typecheck error FS3063: Cannot inherit from erased provided type
77

8-
neg6.fsx(11,3,11,30): typecheck error FS3063: Cannot inherit from erased provided type
8+
neg6.fsx(11,11,11,28): typecheck error FS3063: Cannot inherit from erased provided type
99

10-
neg6.fsx(14,3,14,35): typecheck error FS3063: Cannot inherit from erased provided type
10+
neg6.fsx(14,11,14,33): typecheck error FS3063: Cannot inherit from erased provided type
1111

12-
neg6.fsx(14,3,14,35): typecheck error FS3063: Cannot inherit from erased provided type
12+
neg6.fsx(14,11,14,33): typecheck error FS3063: Cannot inherit from erased provided type

tests/fsharpqa/Source/Conformance/ObjectOrientedTypeDefinitions/ClassTypes/Misc/E_RestrictedSuperTypes.fs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
//<Expects id="FS0771" span="(22,16)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
66
//<Expects id="FS0771" span="(23,16)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
77
//<Expects id="FS0771" span="(24,16)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
8-
//<Expects id="FS0771" span="(26,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
9-
//<Expects id="FS0771" span="(26,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
10-
//<Expects id="FS0771" span="(27,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
11-
//<Expects id="FS0771" span="(27,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
12-
//<Expects id="FS0771" span="(28,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
13-
//<Expects id="FS0771" span="(28,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
14-
//<Expects id="FS0771" span="(29,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
15-
//<Expects id="FS0771" span="(29,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
16-
//<Expects id="FS0771" span="(30,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
17-
//<Expects id="FS0771" span="(30,17)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
8+
//<Expects id="FS0771" span="(26,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
9+
//<Expects id="FS0771" span="(26,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
10+
//<Expects id="FS0771" span="(27,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
11+
//<Expects id="FS0771" span="(27,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
12+
//<Expects id="FS0771" span="(28,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
13+
//<Expects id="FS0771" span="(28,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
14+
//<Expects id="FS0771" span="(29,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
15+
//<Expects id="FS0771" span="(29,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
16+
//<Expects id="FS0771" span="(30,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
17+
//<Expects id="FS0771" span="(30,25)" status="error">The types System\.ValueType, System\.Enum, System\.Delegate, System\.MulticastDelegate and System\.Array cannot be used as super types in an object expression or class</Expects>
1818

1919
#light
2020
let o1 = { new System.ValueType with member x.ToString() = "" }

tests/fsharpqa/Source/Conformance/ObjectOrientedTypeDefinitions/TypeKindInference/infer_interface002e.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// #Regression #Conformance #ObjectOrientedTypes #TypeInference
22
// attribute must match inferred type
33
//<Expects id="FS0927" span="(16,6-16,15)" status="error">The kind of the type specified by its attributes does not match the kind implied by its definition</Expects>
4-
//<Expects id="FS0931" span="(20,4-20,20)" status="error">Structs, interfaces, enums and delegates cannot inherit from other types</Expects>
4+
//<Expects id="FS0931" span="(20,12-20,20)" status="error">Structs, interfaces, enums and delegates cannot inherit from other types</Expects>
55
//<Expects id="FS0946" span="(20,12-20,20)" status="error">Cannot inherit from interface type\. Use interface \.\.\. with instead</Expects>
66
//<Expects id="FS0927" span="(28,6-28,15)" status="error">The kind of the type specified by its attributes does not match the kind implied by its definition</Expects>
7-
//<Expects id="FS0931" span="(32,4-32,20)" status="error">Structs, interfaces, enums and delegates cannot inherit from other types</Expects>
7+
//<Expects id="FS0931" span="(32,12-32,20)" status="error">Structs, interfaces, enums and delegates cannot inherit from other types</Expects>
88
//<Expects id="FS0946" span="(32,12-32,20)" status="error">Cannot inherit from interface type\. Use interface \.\.\. with instead</Expects>
99

1010
// An interface

0 commit comments

Comments
 (0)