Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PoC %typeof#7870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
zth wants to merge4 commits intomaster
base:master
Choose a base branch
Loading
frompoc-typeof
Draft
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
simple %typeof poc
  • Loading branch information
@zth
zth committedSep 10, 2025
commit7d7016b7588bd3db038d3e84de14daacadfb0975
22 changes: 20 additions & 2 deletionscompiler/ml/typetexp.ml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -565,8 +565,26 @@ and transl_type_aux env policy styp =
pack_txt = p;
})
ty
| Ptyp_extension ext ->
raise (Error_forward (Builtin_attributes.error_of_extension ext))
| Ptyp_extension ext -> (
match ext with
| ({txt = "typeof"; loc = ext_loc}, payload) -> (
(* %typeof payload must be a single identifier *)
match Ast_payload.as_ident payload with
| Some ({txt = lid; loc = lid_loc} as _ident) -> (
(* Lookup the value and embed a generic instance of its type.
Using a generic instance avoids capturing weak (non-generalized)
type variables from the value into a type position. *)
let (_path, desc) = find_value env lid_loc lid in
let ty = Ctype.generic_instance env desc.val_type in
(* Build a core_type node carrying the looked up type; we mark the
desc as any since downstream only consults ctyp_type for typing. *)
ctyp Ttyp_any ty)
| None ->
let msg =
"%%typeof expects an identifier. Example: type t = %typeof(x)"
in
raise (Error_forward (Location.error ~loc:ext_loc msg)))
| _ -> raise (Error_forward (Builtin_attributes.error_of_extension ext)))

and transl_poly_type env policy t =
transl_type env policy (Ast_helper.Typ.force_poly t)
Expand Down
5 changes: 5 additions & 0 deletionstests/analysis_tests/tests/src/Hover.res
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -281,3 +281,8 @@ module Arr = Belt.Array

type aliased = variant
// ^hov

let myFn = (a, b) => a ++ b->Int.toString

type fnType = %typeof(myFn)
// ^hov
3 changes: 3 additions & 0 deletionstests/analysis_tests/tests/src/expected/Hover.res.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -348,3 +348,6 @@ Hover src/Hover.res 278:8
Hover src/Hover.res 281:6
{"contents": {"kind": "markdown", "value": "```rescript\ntype aliased = variant\n```\n\n---\n\n```\n \n```\n```rescript\ntype variant = CoolVariant | OtherCoolVariant\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C251%2C0%5D)\n"}}

Hover src/Hover.res 286:6
{"contents": {"kind": "markdown", "value": "```rescript\ntype fnType = (string, int) => string\n```"}}

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@

We've found a bug for you!
/.../fixtures/typeof_mismatch.res:6:28

4 │
5 │ let f: fnType = myFn
6 │ let ff: fnType = (a, b) => a->Int.toString + b

This has type: string
But this function argument is expecting: int

You can convert string to int with Int.fromString.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
let myFn = (a, b) => a ++ b->Int.toString

type fnType = %typeof(myFn)

let f: fnType = myFn
let ff: fnType = (a, b) => a->Int.toString + b
5 changes: 5 additions & 0 deletionstests/tests/src/Typeof.res
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
let myFn = (a, b) => a ++ b->Int.toString

type fnType = %typeof(myFn)

let f: fnType = myFn

[8]ページ先頭

©2009-2025 Movatter.jp