From bbdb1667ea02bf2737f01b31866040360068bced Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Tue, 14 Nov 2023 17:31:14 +0100 Subject: [PATCH 1/5] showcase stack overflow bug with polyvariants --- analysis/tests/src/PolyRec.res | 14 ++++++++++++++ analysis/tests/src/expected/PolyRec.res.txt | 2 ++ 2 files changed, 16 insertions(+) create mode 100644 analysis/tests/src/PolyRec.res create mode 100644 analysis/tests/src/expected/PolyRec.res.txt diff --git a/analysis/tests/src/PolyRec.res b/analysis/tests/src/PolyRec.res new file mode 100644 index 000000000..c2fdbdf52 --- /dev/null +++ b/analysis/tests/src/PolyRec.res @@ -0,0 +1,14 @@ +let rec sum = x => + switch x { + | #Leaf => 0 + | #Node(value, left, right) => value + left->sum + right->sum + } + +let myTree = #Node( + 1, + #Node(2, #Node(4, #Leaf, #Leaf), #Node(6, #Leaf, #Leaf)), + #Node(3, #Node(5, #Leaf, #Leaf), #Node(7, #Leaf, #Leaf)), +) + +let () = myTree->sum->Js.log +// ^hov diff --git a/analysis/tests/src/expected/PolyRec.res.txt b/analysis/tests/src/expected/PolyRec.res.txt new file mode 100644 index 000000000..aabf34d3e --- /dev/null +++ b/analysis/tests/src/expected/PolyRec.res.txt @@ -0,0 +1,2 @@ +Hover src/PolyRec.res 12:10 +Fatal error: exception Stack overflow From c355552be9b2af5915e4d7760869a77a42b7945f Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Tue, 14 Nov 2023 19:36:07 +0100 Subject: [PATCH 2/5] Mark where the stack overflow happens. --- analysis/src/PrintType.ml | 4 +++- analysis/tests/src/expected/PolyRec.res.txt | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/analysis/src/PrintType.ml b/analysis/src/PrintType.ml index f06239f60..0530e7a35 100644 --- a/analysis/src/PrintType.ml +++ b/analysis/src/PrintType.ml @@ -1,7 +1,9 @@ let printExpr ?(lineWidth = 60) typ = Printtyp.reset_names (); + try Res_doc.toString ~width:lineWidth - (Res_outcome_printer.printOutTypeDoc (Printtyp.tree_of_typexp false typ)) + (Res_outcome_printer.printOutTypeDoc (Printtyp.tree_of_typexp false typ (* HERE *))) + with _ -> "Error: could not print type" let printDecl ?printNameAsIs ~recStatus name decl = Printtyp.reset_names (); diff --git a/analysis/tests/src/expected/PolyRec.res.txt b/analysis/tests/src/expected/PolyRec.res.txt index aabf34d3e..f1e358c15 100644 --- a/analysis/tests/src/expected/PolyRec.res.txt +++ b/analysis/tests/src/expected/PolyRec.res.txt @@ -1,2 +1,3 @@ Hover src/PolyRec.res 12:10 -Fatal error: exception Stack overflow +{"contents": {"kind": "markdown", "value": "```rescript\nError: could not print type\n```"}} + From ed2bbfe4209a3883990f00f2f5c6a2fb1d53899d Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Tue, 14 Nov 2023 20:04:47 +0100 Subject: [PATCH 3/5] try something --- analysis/src/PrintType.ml | 1 + .../tests/src/expected/CompletionPattern.res.txt | 12 ++++++------ analysis/tests/src/expected/Hover.res.txt | 4 ++-- analysis/tests/src/expected/PolyRec.res.txt | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/analysis/src/PrintType.ml b/analysis/src/PrintType.ml index 0530e7a35..f02af4c36 100644 --- a/analysis/src/PrintType.ml +++ b/analysis/src/PrintType.ml @@ -1,5 +1,6 @@ let printExpr ?(lineWidth = 60) typ = Printtyp.reset_names (); + Printtyp.reset_and_mark_loops typ; try Res_doc.toString ~width:lineWidth (Res_outcome_printer.printOutTypeDoc (Printtyp.tree_of_typexp false typ (* HERE *))) diff --git a/analysis/tests/src/expected/CompletionPattern.res.txt b/analysis/tests/src/expected/CompletionPattern.res.txt index 56f0ad0f9..850c64919 100644 --- a/analysis/tests/src/expected/CompletionPattern.res.txt +++ b/analysis/tests/src/expected/CompletionPattern.res.txt @@ -129,7 +129,7 @@ Path f "label": "optThird", "kind": 5, "tags": [], - "detail": "optThird: option<[#second(someRecord) | #first]>\n\nsomeRecord", + "detail": "optThird: option<[#first | #second(someRecord)]>\n\nsomeRecord", "documentation": null }, { "label": "nest", @@ -150,7 +150,7 @@ Path f "label": "optThird", "kind": 5, "tags": [], - "detail": "optThird: option<[#second(someRecord) | #first]>\n\nsomeRecord", + "detail": "optThird: option<[#first | #second(someRecord)]>\n\nsomeRecord", "documentation": null }, { "label": "nest", @@ -189,7 +189,7 @@ Path z "label": "optThird", "kind": 5, "tags": [], - "detail": "optThird: option<[#second(someRecord) | #first]>\n\nsomeRecord", + "detail": "optThird: option<[#first | #second(someRecord)]>\n\nsomeRecord", "documentation": null }] @@ -267,7 +267,7 @@ Path f "label": "optThird", "kind": 5, "tags": [], - "detail": "optThird: option<[#second(someRecord) | #first]>\n\nsomeRecord", + "detail": "optThird: option<[#first | #second(someRecord)]>\n\nsomeRecord", "documentation": null }, { "label": "nest", @@ -360,7 +360,7 @@ Path z "label": "optThird", "kind": 5, "tags": [], - "detail": "optThird: option<[#second(someRecord) | #first]>\n\nsomeRecord", + "detail": "optThird: option<[#first | #second(someRecord)]>\n\nsomeRecord", "documentation": null }, { "label": "nest", @@ -451,7 +451,7 @@ Path b "label": "optThird", "kind": 5, "tags": [], - "detail": "optThird: option<[#second(someRecord) | #first]>\n\nsomeRecord", + "detail": "optThird: option<[#first | #second(someRecord)]>\n\nsomeRecord", "documentation": null }, { "label": "nest", diff --git a/analysis/tests/src/expected/Hover.res.txt b/analysis/tests/src/expected/Hover.res.txt index 8503e0b48..9fdad7048 100644 --- a/analysis/tests/src/expected/Hover.res.txt +++ b/analysis/tests/src/expected/Hover.res.txt @@ -77,10 +77,10 @@ Hover src/Hover.res 106:21 {"contents": {"kind": "markdown", "value": "```rescript\nint\n```"}} Hover src/Hover.res 116:16 -{"contents": {"kind": "markdown", "value": "```rescript\nAA.cond<[< #str(string)]> => AA.cond<[< #str(string)]>\n```\n\n---\n\n```\n \n```\n```rescript\ntype AA.cond<'a> = 'a\n constraint 'a = [< #str(string)]\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C110%2C2%5D)\n"}} +{"contents": {"kind": "markdown", "value": "```rescript\nAA.cond<([< #str(string)] as 'a)> => AA.cond<'a>\n```\n\n---\n\n```\n \n```\n```rescript\ntype AA.cond<'a> = 'a\n constraint 'a = [< #str(string)]\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C110%2C2%5D)\n"}} Hover src/Hover.res 119:25 -{"contents": {"kind": "markdown", "value": "```rescript\nAA.cond<[< #str(string)]> => AA.cond<[< #str(string)]>\n```\n\n---\n\n```\n \n```\n```rescript\ntype AA.cond<'a> = 'a\n constraint 'a = [< #str(string)]\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C110%2C2%5D)\n"}} +{"contents": {"kind": "markdown", "value": "```rescript\nAA.cond<([< #str(string)] as 'a)> => AA.cond<'a>\n```\n\n---\n\n```\n \n```\n```rescript\ntype AA.cond<'a> = 'a\n constraint 'a = [< #str(string)]\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C110%2C2%5D)\n"}} Hover src/Hover.res 122:3 Nothing at that position. Now trying to use completion. diff --git a/analysis/tests/src/expected/PolyRec.res.txt b/analysis/tests/src/expected/PolyRec.res.txt index f1e358c15..64c790174 100644 --- a/analysis/tests/src/expected/PolyRec.res.txt +++ b/analysis/tests/src/expected/PolyRec.res.txt @@ -1,3 +1,3 @@ Hover src/PolyRec.res 12:10 -{"contents": {"kind": "markdown", "value": "```rescript\nError: could not print type\n```"}} +{"contents": {"kind": "markdown", "value": "```rescript\n([#Leaf | #Node(int, 'a, 'a)] as 'a)\n```"}} From 877588bc9f7ac2a377ea790fc37ddbbe7dd3ea25 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Mon, 18 Dec 2023 18:42:08 +0100 Subject: [PATCH 4/5] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dab1057f5..9a981e20e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Proper default for `"uncurried"` in V11 projects. https://github.com/rescript-lang/rescript-vscode/pull/867 - Treat `result` type as a proper built in type. https://github.com/rescript-lang/rescript-vscode/pull/860 - Fix infinite loop when resolving inferred completions when several values in scope has the same name. https://github.com/rescript-lang/rescript-vscode/pull/869 +- Fix crash when trying to print recursive polymorphic variants without a concrete definition. https://github.com/rescript-lang/rescript-vscode/pull/851 #### :nail_care: Polish From 6b78f94d4dd9a03c02ef7105c9016f3aa9e15fcf Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Mon, 18 Dec 2023 18:43:17 +0100 Subject: [PATCH 5/5] remove debug --- analysis/src/PrintType.ml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/analysis/src/PrintType.ml b/analysis/src/PrintType.ml index f02af4c36..3fe104398 100644 --- a/analysis/src/PrintType.ml +++ b/analysis/src/PrintType.ml @@ -1,10 +1,8 @@ let printExpr ?(lineWidth = 60) typ = Printtyp.reset_names (); Printtyp.reset_and_mark_loops typ; - try Res_doc.toString ~width:lineWidth - (Res_outcome_printer.printOutTypeDoc (Printtyp.tree_of_typexp false typ (* HERE *))) - with _ -> "Error: could not print type" + (Res_outcome_printer.printOutTypeDoc (Printtyp.tree_of_typexp false typ)) let printDecl ?printNameAsIs ~recStatus name decl = Printtyp.reset_names ();