Skip to content

Commit cb177b1

Browse files
nojafedgarfgpdsymevzarytovskii
authored
Print int[] as int array. (#13700)
Co-authored-by: Edgar <[email protected]> Co-authored-by: Don Syme <[email protected]> Co-authored-by: Vlad Zarytovskii <[email protected]>
1 parent 14f9278 commit cb177b1

File tree

32 files changed

+984
-863
lines changed

32 files changed

+984
-863
lines changed

src/Compiler/Checking/CheckExpressions.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4303,6 +4303,9 @@ and TcTypeOrMeasure kindOpt (cenv: cenv) newOk checkConstraints occ (iwsam: Warn
43034303
| SynType.LongIdent synLongId ->
43044304
TcLongIdentType kindOpt cenv newOk checkConstraints occ iwsam env tpenv synLongId
43054305

4306+
| MultiDimensionArrayType (rank, elemTy, m) ->
4307+
TcElementType cenv newOk checkConstraints occ env tpenv rank elemTy m
4308+
43064309
| SynType.App (StripParenTypes (SynType.LongIdent longId), _, args, _, _, postfix, m) ->
43074310
TcLongIdentAppType kindOpt cenv newOk checkConstraints occ iwsam env tpenv longId postfix args m
43084311

src/Compiler/Checking/NicePrint.fs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ module internal PrintUtilities =
180180
let isArray = not prefix && isArrayTyconRef denv.g tcref
181181
let demangled =
182182
if isArray then
183-
tcref.DisplayNameCore // no backticks for arrays "int[]"
183+
let numberOfCommas = tcref.CompiledName |> Seq.filter (fun c -> c = ',') |> Seq.length
184+
if numberOfCommas = 0 then
185+
"array"
186+
else
187+
$"array{numberOfCommas + 1}d"
184188
else
185189
let name =
186190
if denv.includeStaticParametersInTypeNames then
@@ -198,11 +202,7 @@ module internal PrintUtilities =
198202
tagEntityRefName denv tcref demangled
199203
|> mkNav tcref.DefinitionRange
200204

201-
let tyconTextL =
202-
if isArray then
203-
tyconTagged |> rightL
204-
else
205-
tyconTagged |> wordL
205+
let tyconTextL = tyconTagged |> wordL
206206

207207
if denv.shortTypeNames then
208208
tyconTextL
@@ -873,6 +873,16 @@ module PrintTypes =
873873
| [arg] -> layoutTypeWithInfoAndPrec denv env 2 arg ^^ tcL
874874
| args -> bracketIfL (prec <= 1) (bracketL (layoutTypesWithInfoAndPrec denv env 2 (sepL (tagPunctuation ",")) args) --- tcL)
875875

876+
and layoutTypeForGenericMultidimensionalArrays denv env prec tcref innerT level =
877+
let innerLayout = layoutTypeWithInfoAndPrec denv env prec innerT
878+
879+
let arrayLayout =
880+
tagEntityRefName denv tcref $"array{level}d"
881+
|> mkNav tcref.DefinitionRange
882+
|> wordL
883+
884+
innerLayout ^^ arrayLayout
885+
876886
/// Layout a type, taking precedence into account to insert brackets where needed
877887
and layoutTypeWithInfoAndPrec denv env prec ty =
878888
let g = denv.g
@@ -893,6 +903,10 @@ module PrintTypes =
893903
// Always prefer 'float' to 'float<1>'
894904
| TType_app (tc, args, _) when tc.IsMeasureableReprTycon && List.forall (isDimensionless g) args ->
895905
layoutTypeWithInfoAndPrec denv env prec (reduceTyconRefMeasureableOrProvided g tc args)
906+
907+
// Special case for nested array<array<'t>> shape
908+
| TTypeMultiDimensionalArrayAsGeneric (tcref, innerT, level) ->
909+
layoutTypeForGenericMultidimensionalArrays denv env prec tcref innerT level
896910

897911
// Layout a type application
898912
| TType_ucase (UnionCaseRef(tc, _), args)

src/Compiler/SyntaxTree/SyntaxTreeOps.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,3 +1059,19 @@ let getTypeFromTuplePath (path: SynTupleTypeSegment list) : SynType list =
10591059
|> List.choose (function
10601060
| SynTupleTypeSegment.Type t -> Some t
10611061
| _ -> None)
1062+
1063+
let (|MultiDimensionArrayType|_|) (t: SynType) =
1064+
match t with
1065+
| SynType.App (StripParenTypes (SynType.LongIdent (SynLongIdent ([ identifier ], _, _))), _, [ elementType ], _, _, true, m) ->
1066+
if System.Text.RegularExpressions.Regex.IsMatch(identifier.idText, "^array\d\d?d$") then
1067+
let rank =
1068+
identifier.idText
1069+
|> Seq.filter System.Char.IsDigit
1070+
|> Seq.toArray
1071+
|> System.String
1072+
|> int
1073+
1074+
Some(rank, elementType, m)
1075+
else
1076+
None
1077+
| _ -> None

src/Compiler/SyntaxTree/SyntaxTreeOps.fsi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,5 @@ val normalizeTupleExpr: exprs: SynExpr list -> commas: range list -> SynExpr lis
353353
val desugarGetSetMembers: memberDefns: SynMemberDefns -> SynMemberDefns
354354

355355
val getTypeFromTuplePath: path: SynTupleTypeSegment list -> SynType list
356+
357+
val (|MultiDimensionArrayType|_|): t: SynType -> (int * SynType * range) option

src/Compiler/TypedTree/TypedTreeOps.fs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10384,7 +10384,7 @@ let (|EmptyModuleOrNamespaces|_|) (moduleOrNamespaceContents: ModuleOrNamespaceC
1038410384
| ModuleOrNamespaceContents.TMDefRec _
1038510385
| ModuleOrNamespaceContents.TMDefs _ -> true
1038610386
| _ -> false)
10387-
10387+
1038810388
let emptyModuleOrNamespaces =
1038910389
defs
1039010390
|> List.choose (function
@@ -10407,3 +10407,17 @@ let (|EmptyModuleOrNamespaces|_|) (moduleOrNamespaceContents: ModuleOrNamespaceC
1040710407
else
1040810408
None
1040910409
| _ -> None
10410+
10411+
let (|TTypeMultiDimensionalArrayAsGeneric|_|) (t: TType) =
10412+
let rec (|Impl|_|) t =
10413+
match t with
10414+
| TType_app(tc, [Impl(outerTc, innerT, currentLevel)], _) when tc.DisplayNameCore = "array" ->
10415+
Some (outerTc, innerT, currentLevel + 1)
10416+
| TType_app(tc, [arg], _) when tc.DisplayNameCore = "array" ->
10417+
Some (tc, arg, 1)
10418+
| _ -> None
10419+
10420+
match t with
10421+
| Impl (tc, arg, level) ->
10422+
if level > 2 then Some (tc, arg, level) else None
10423+
| _ -> None

src/Compiler/TypedTree/TypedTreeOps.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,3 +2687,6 @@ type TraitConstraintInfo with
26872687
/// This will match anything that does not have any types or bindings.
26882688
val (|EmptyModuleOrNamespaces|_|):
26892689
moduleOrNamespaceContents: ModuleOrNamespaceContents -> (ModuleOrNamespace list) option
2690+
2691+
/// Captures an application type with a multi-dimensional array as postfix.
2692+
val (|TTypeMultiDimensionalArrayAsGeneric|_|): t: TType -> (TyconRef * TType * int) option

tests/FSharp.Compiler.ComponentTests/Conformance/DeclarationElements/CustomAttributes/Basic/Basic.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module Basic =
103103
|> verifyCompile
104104
|> shouldFail
105105
|> withDiagnostics [
106-
(Error 1, Line 10, Col 3, Line 10, Col 59, "This expression was expected to have type\n 'int[]' \nbut here has type\n 'unit' ")
106+
(Error 1, Line 10, Col 3, Line 10, Col 59, "This expression was expected to have type\n 'int array' \nbut here has type\n 'unit' ")
107107
(Error 267, Line 10, Col 3, Line 10, Col 59, "This is not a valid constant expression or custom attribute value")
108108
(Error 850, Line 10, Col 3, Line 10, Col 59, "This attribute cannot be used in this version of F#")
109109
(Error 850, Line 13, Col 3, Line 13, Col 52, "This attribute cannot be used in this version of F#")

tests/FSharp.Compiler.ComponentTests/Conformance/DeclarationElements/MemberDefinitions/MethodsAndProperties/MethodsAndProperties.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ module MethodsAndProperties =
9797
|> verifyCompile
9898
|> shouldFail
9999
|> withDiagnostics [
100-
(Error 1, Line 11, Col 24, Line 11, Col 33, "This expression was expected to have type\n ''a[,]' \nbut here has type\n 'int[]' ")
101-
(Error 1, Line 12, Col 25, Line 12, Col 32, "This expression was expected to have type\n ''a[]' \nbut here has type\n 'int[,]' ")
102-
(Error 1, Line 13, Col 26, Line 13, Col 37, "This expression was expected to have type\n ''a[]' \nbut here has type\n 'int[,,]' ")
103-
(Error 1, Line 14, Col 27, Line 14, Col 38, "This expression was expected to have type\n ''a[]' \nbut here has type\n 'int[,,,]' ")
100+
(Error 1, Line 11, Col 24, Line 11, Col 33, "This expression was expected to have type\n ''a array2d' \nbut here has type\n 'int array' ")
101+
(Error 1, Line 12, Col 25, Line 12, Col 32, "This expression was expected to have type\n ''a array' \nbut here has type\n 'int array2d' ")
102+
(Error 1, Line 13, Col 26, Line 13, Col 37, "This expression was expected to have type\n ''a array' \nbut here has type\n 'int array3d' ")
103+
(Error 1, Line 14, Col 27, Line 14, Col 38, "This expression was expected to have type\n ''a array' \nbut here has type\n 'int array4d' ")
104104
]
105105

106106
// SOURCE=E_IndexerArityMismatch02.fs SCFLAGS="--test:ErrorRanges --flaterrors" # E_IndexerArityMismatch02.fs
@@ -110,10 +110,10 @@ module MethodsAndProperties =
110110
|> verifyCompile
111111
|> shouldFail
112112
|> withDiagnostics [
113-
(Error 1, Line 11, Col 24, Line 11, Col 35, "This expression was expected to have type\n ''a[,,]' \nbut here has type\n 'int[]' ")
114-
(Error 1, Line 12, Col 25, Line 12, Col 32, "This expression was expected to have type\n ''a[]' \nbut here has type\n 'int[,]' ")
115-
(Error 1, Line 13, Col 27, Line 13, Col 38, "This expression was expected to have type\n ''a[,,]' \nbut here has type\n 'int[,,,]' ")
116-
(Error 1, Line 14, Col 27, Line 14, Col 36, "This expression was expected to have type\n ''a[,]' \nbut here has type\n 'int[,,,]' ")
113+
(Error 1, Line 11, Col 24, Line 11, Col 35, "This expression was expected to have type\n ''a array3d' \nbut here has type\n 'int array' ")
114+
(Error 1, Line 12, Col 25, Line 12, Col 32, "This expression was expected to have type\n ''a array' \nbut here has type\n 'int array2d' ")
115+
(Error 1, Line 13, Col 27, Line 13, Col 38, "This expression was expected to have type\n ''a array3d' \nbut here has type\n 'int array4d' ")
116+
(Error 1, Line 14, Col 27, Line 14, Col 36, "This expression was expected to have type\n ''a array2d' \nbut here has type\n 'int array4d' ")
117117
]
118118

119119
// SOURCE=E_IndexerIndeterminateType01.fs SCFLAGS=--test:ErrorRanges # E_IndexerIndeterminateType01.fs

tests/FSharp.Compiler.ComponentTests/Conformance/RecordTypes/RecordTypes.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module RecordTypes =
118118
|> verifyTypeCheck
119119
|> shouldFail
120120
|> withDiagnostics [
121-
(Error 1, Line 7, Col 17, Line 7, Col 47, "This expression was expected to have type\n 'int[]' \nbut here has type\n 'RecType' ")
121+
(Error 1, Line 7, Col 17, Line 7, Col 47, "This expression was expected to have type\n 'int array' \nbut here has type\n 'RecType' ")
122122
]
123123

124124
// SOURCE=E_RecordsNotNull01.fs # E_RecordsNotNull01.fs

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
<Compile Include="Signatures\TestHelpers.fs" />
200200
<Compile Include="Signatures\ModuleOrNamespaceTests.fs" />
201201
<Compile Include="Signatures\RecordTests.fs" />
202+
<Compile Include="Signatures\ArrayTests.fs" />
202203
</ItemGroup>
203204
<ItemGroup>
204205
<Content Include="resources\**" CopyToOutputDirectory="Never" CopyToPublishDirectory="PreserveNewest" />

0 commit comments

Comments
 (0)