Skip to content

Commit

Permalink
Merge pull request #6170 from roc-lang/inspect-to-str
Browse files Browse the repository at this point in the history
Add `Inspect.toStr`
  • Loading branch information
bhansconnect authored Dec 3, 2023
2 parents 22c5241 + e1c850e commit a187d14
Show file tree
Hide file tree
Showing 29 changed files with 1,115 additions and 1,077 deletions.
9 changes: 7 additions & 2 deletions crates/compiler/builtins/roc/Inspect.roc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ interface Inspect
custom,
apply,
toInspector,
DbgFormatter,
toDbgStr,
toStr,
]
imports [
Bool.{ Bool },
Expand Down Expand Up @@ -99,6 +98,12 @@ inspect = \val ->
(@Inspector valFn) = toInspector val
valFn (init {})

toStr : val -> Str where val implements Inspect
toStr = \val ->
val
|> inspect
|> toDbgStr

# The current default formatter for inspect.
# This just returns a simple string for debugging.
# More powerful formatters will likely be wanted in the future.
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/builtins/roc/main.roc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package "builtins"
exposes [Str, Num, Bool, Result, List, Dict, Set, Decode, Encode, Hash, Box, TotallyNotJson]
exposes [Str, Num, Bool, Result, List, Dict, Set, Decode, Encode, Hash, Box, TotallyNotJson, Inspect]
packages {}
24 changes: 4 additions & 20 deletions crates/compiler/can/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,7 @@ pub fn desugar_expr<'a>(
}
Dbg(condition, continuation) => {
// Desugars a `dbg x` statement into essentially
// TODO(#6167): Switch back to `Inspect.toInspector x |> Inspect.apply (Inspect.init {}) |> Inspect.toDbgStr |> LowLevelDbg`
// Inspect.inspect x |> Inspect.toDbgStr |> LowLevelDbg
// Inspect.toStr x |> LowLevelDbg
let desugared_continuation = &*arena.alloc(desugar_expr(
arena,
continuation,
Expand All @@ -576,10 +575,10 @@ pub fn desugar_expr<'a>(
));

let region = condition.region;
// Inspect.toInspector x
// Inspect.toStr x
let inspect_fn = Var {
module_name: ModuleName::INSPECT,
ident: "inspect",
ident: "toStr",
};
let loc_inspect_fn_var = arena.alloc(Loc {
value: inspect_fn,
Expand All @@ -588,23 +587,8 @@ pub fn desugar_expr<'a>(
let desugared_inspect_args =
arena.alloc([desugar_expr(arena, condition, src, line_info, module_path)]);

let formatter = arena.alloc(Loc {
value: Apply(loc_inspect_fn_var, desugared_inspect_args, CalledVia::Space),
region,
});

// |> Inspect.toDbgStr
let to_dbg_str = Var {
module_name: ModuleName::INSPECT,
ident: "toDbgStr",
};
let loc_to_dbg_str_fn_var = arena.alloc(Loc {
value: to_dbg_str,
region,
});
let to_dbg_str_args = arena.alloc([&*formatter]);
let dbg_str = arena.alloc(Loc {
value: Apply(loc_to_dbg_str_fn_var, to_dbg_str_args, CalledVia::Space),
value: Apply(loc_inspect_fn_var, desugared_inspect_args, CalledVia::Space),
region,
});

Expand Down
3 changes: 1 addition & 2 deletions crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,8 +1631,7 @@ define_builtins! {
31 INSPECT_APPLY: "apply"
32 INSPECT_TO_INSPECTOR: "toInspector"
33 INSPECT_NAT: "nat"
34 INSPECT_DBG_FORMATTER: "DbgFormatter" exposed_type=true
35 INSPECT_TO_DBG_STR: "toDbgStr"
34 INSPECT_TO_STR: "toStr"
}
15 JSON: "TotallyNotJson" => {
0 JSON_JSON: "TotallyNotJson"
Expand Down
62 changes: 31 additions & 31 deletions crates/compiler/test_gen/src/gen_abilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2203,9 +2203,9 @@ mod inspect {
app "test" provides [main] to "./platform"
main = [
Inspect.inspect Bool.true,
Inspect.inspect Bool.false,
] |> List.map Inspect.toDbgStr |> Str.joinWith ", "
Inspect.toStr Bool.true,
Inspect.toStr Bool.false,
] |> Str.joinWith ", "
"#
),
RocStr::from("Bool.true, Bool.false"),
Expand All @@ -2222,22 +2222,22 @@ mod inspect {
app "test" provides [main] to "./platform"
main = [
Inspect.inspect 0, # Num a
Inspect.inspect 1u8, # U8
Inspect.inspect 2i8, # I8
Inspect.inspect 3u16, # U16
Inspect.inspect 4i16, # I16
Inspect.inspect 5u32, # U32
Inspect.inspect 6i32, # I32
Inspect.inspect 7u64, # U64
Inspect.inspect 8i64, # I64
Inspect.inspect 9u128, # U128
Inspect.inspect 10i128, # I128
Inspect.inspect 0.5, # Frac a
Inspect.inspect 1.5f32, # F32
Inspect.inspect 2.2f64, # F64
Inspect.inspect (1.1dec + 2.2), # Dec
] |> List.map Inspect.toDbgStr |> Str.joinWith ", "
Inspect.toStr 0, # Num a
Inspect.toStr 1u8, # U8
Inspect.toStr 2i8, # I8
Inspect.toStr 3u16, # U16
Inspect.toStr 4i16, # I16
Inspect.toStr 5u32, # U32
Inspect.toStr 6i32, # I32
Inspect.toStr 7u64, # U64
Inspect.toStr 8i64, # I64
Inspect.toStr 9u128, # U128
Inspect.toStr 10i128, # I128
Inspect.toStr 0.5, # Frac a
Inspect.toStr 1.5f32, # F32
Inspect.toStr 2.2f64, # F64
Inspect.toStr (1.1dec + 2.2), # Dec
] |> Str.joinWith ", "
"#
),
RocStr::from("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0.5, 1.5, 2.2, 3.3"),
Expand All @@ -2254,12 +2254,12 @@ mod inspect {
app "test" provides [main] to "./platform"
main = [
Inspect.inspect [0, 1, 2], # List (Num *)
Inspect.inspect [1, 0x2, 3], # List (Int *)
Inspect.inspect [0.1 + 0.2, 0.4], # List (Frac *)
Inspect.inspect [1u8, 2u8], # List U8
Inspect.inspect ["foo"], # List Str
] |> List.map Inspect.toDbgStr |> Str.joinWith ", "
Inspect.toStr [0, 1, 2], # List (Num *)
Inspect.toStr [1, 0x2, 3], # List (Int *)
Inspect.toStr [0.1 + 0.2, 0.4], # List (Frac *)
Inspect.toStr [1u8, 2u8], # List U8
Inspect.toStr ["foo"], # List Str
] |> Str.joinWith ", "
"#
),
RocStr::from("[0, 1, 2], [1, 2, 3], [0.3, 0.4], [1, 2], [\"foo\"]"),
Expand All @@ -2276,10 +2276,10 @@ mod inspect {
app "test" provides [main] to "./platform"
main = [
Inspect.inspect "",
Inspect.inspect "a small string",
Inspect.inspect "an extraordinarily long string - so long it's on the heap!",
] |> List.map Inspect.toDbgStr |> Str.joinWith ", "
Inspect.toStr "",
Inspect.toStr "a small string",
Inspect.toStr "an extraordinarily long string - so long it's on the heap!",
] |> Str.joinWith ", "
"#
),
RocStr::from(
Expand All @@ -2299,7 +2299,7 @@ mod inspect {
Op := {}
main = Inspect.toDbgStr (Inspect.inspect (@Op {}))
main = Inspect.toStr (@Op {})
"#
),
RocStr::from(r#"<opaque>"#),
Expand All @@ -2317,7 +2317,7 @@ mod inspect {
Op := {}
late = \a -> Inspect.toDbgStr (Inspect.inspect a)
late = \a -> Inspect.toStr a
main = late (@Op {})
"#
Expand Down
62 changes: 33 additions & 29 deletions crates/compiler/test_mono/generated/dbg_in_expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,52 @@ procedure Bool.2 ():
let Bool.23 : Int1 = true;
ret Bool.23;

procedure Inspect.249 (Inspect.250, Inspect.248):
procedure Inspect.251 (Inspect.252, Inspect.250):
let Inspect.327 : Str = "\"";
let Inspect.326 : Str = CallByName Inspect.61 Inspect.252 Inspect.327;
let Inspect.322 : Str = CallByName Inspect.61 Inspect.326 Inspect.250;
let Inspect.323 : Str = "\"";
let Inspect.322 : Str = CallByName Inspect.61 Inspect.250 Inspect.323;
let Inspect.318 : Str = CallByName Inspect.61 Inspect.322 Inspect.248;
let Inspect.319 : Str = "\"";
let Inspect.317 : Str = CallByName Inspect.61 Inspect.318 Inspect.319;
ret Inspect.317;
let Inspect.321 : Str = CallByName Inspect.61 Inspect.322 Inspect.323;
ret Inspect.321;

procedure Inspect.30 (Inspect.147):
ret Inspect.147;
procedure Inspect.30 (Inspect.148):
ret Inspect.148;

procedure Inspect.35 (Inspect.300):
ret Inspect.300;
procedure Inspect.34 (Inspect.153):
let Inspect.309 : Str = CallByName Inspect.5 Inspect.153;
let Inspect.308 : Str = CallByName Inspect.62 Inspect.309;
ret Inspect.308;

procedure Inspect.36 (Inspect.304):
let Inspect.311 : Str = "";
ret Inspect.311;
procedure Inspect.36 (Inspect.305):
let Inspect.315 : Str = "";
ret Inspect.315;

procedure Inspect.44 (Inspect.248):
let Inspect.313 : Str = CallByName Inspect.30 Inspect.248;
ret Inspect.313;
procedure Inspect.44 (Inspect.250):
let Inspect.317 : Str = CallByName Inspect.30 Inspect.250;
ret Inspect.317;

procedure Inspect.5 (Inspect.150):
let Inspect.312 : Str = CallByName Inspect.44 Inspect.150;
let Inspect.309 : {} = Struct {};
let Inspect.308 : Str = CallByName Inspect.36 Inspect.309;
let Inspect.307 : Str = CallByName Inspect.249 Inspect.308 Inspect.312;
ret Inspect.307;
procedure Inspect.5 (Inspect.151):
let Inspect.316 : Str = CallByName Inspect.44 Inspect.151;
let Inspect.313 : {} = Struct {};
let Inspect.312 : Str = CallByName Inspect.36 Inspect.313;
let Inspect.311 : Str = CallByName Inspect.251 Inspect.312 Inspect.316;
ret Inspect.311;

procedure Inspect.61 (Inspect.303, Inspect.298):
let Inspect.321 : Str = CallByName Str.3 Inspect.303 Inspect.298;
dec Inspect.298;
ret Inspect.321;
procedure Inspect.61 (Inspect.304, Inspect.300):
let Inspect.325 : Str = CallByName Str.3 Inspect.304 Inspect.300;
dec Inspect.300;
ret Inspect.325;

procedure Inspect.62 (Inspect.306):
ret Inspect.306;

procedure Str.3 (#Attr.2, #Attr.3):
let Str.292 : Str = lowlevel StrConcat #Attr.2 #Attr.3;
ret Str.292;

procedure Test.1 ():
let Test.5 : Str = "";
let Test.4 : Str = CallByName Inspect.5 Test.5;
let Test.0 : Str = CallByName Inspect.35 Test.4;
let Test.4 : Str = "";
let Test.0 : Str = CallByName Inspect.34 Test.4;
dbg Test.0;
dec Test.0;
let Test.3 : Int1 = CallByName Bool.2;
Expand Down
62 changes: 33 additions & 29 deletions crates/compiler/test_mono/generated/dbg_str_followed_by_number.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
procedure Inspect.249 (Inspect.250, Inspect.248):
procedure Inspect.251 (Inspect.252, Inspect.250):
let Inspect.327 : Str = "\"";
let Inspect.326 : Str = CallByName Inspect.61 Inspect.252 Inspect.327;
let Inspect.322 : Str = CallByName Inspect.61 Inspect.326 Inspect.250;
let Inspect.323 : Str = "\"";
let Inspect.322 : Str = CallByName Inspect.61 Inspect.250 Inspect.323;
let Inspect.318 : Str = CallByName Inspect.61 Inspect.322 Inspect.248;
let Inspect.319 : Str = "\"";
let Inspect.317 : Str = CallByName Inspect.61 Inspect.318 Inspect.319;
ret Inspect.317;
let Inspect.321 : Str = CallByName Inspect.61 Inspect.322 Inspect.323;
ret Inspect.321;

procedure Inspect.30 (Inspect.147):
ret Inspect.147;
procedure Inspect.30 (Inspect.148):
ret Inspect.148;

procedure Inspect.35 (Inspect.300):
ret Inspect.300;
procedure Inspect.34 (Inspect.153):
let Inspect.309 : Str = CallByName Inspect.5 Inspect.153;
let Inspect.308 : Str = CallByName Inspect.62 Inspect.309;
ret Inspect.308;

procedure Inspect.36 (Inspect.304):
let Inspect.311 : Str = "";
ret Inspect.311;
procedure Inspect.36 (Inspect.305):
let Inspect.315 : Str = "";
ret Inspect.315;

procedure Inspect.44 (Inspect.248):
let Inspect.313 : Str = CallByName Inspect.30 Inspect.248;
ret Inspect.313;
procedure Inspect.44 (Inspect.250):
let Inspect.317 : Str = CallByName Inspect.30 Inspect.250;
ret Inspect.317;

procedure Inspect.5 (Inspect.150):
let Inspect.312 : Str = CallByName Inspect.44 Inspect.150;
let Inspect.309 : {} = Struct {};
let Inspect.308 : Str = CallByName Inspect.36 Inspect.309;
let Inspect.307 : Str = CallByName Inspect.249 Inspect.308 Inspect.312;
ret Inspect.307;
procedure Inspect.5 (Inspect.151):
let Inspect.316 : Str = CallByName Inspect.44 Inspect.151;
let Inspect.313 : {} = Struct {};
let Inspect.312 : Str = CallByName Inspect.36 Inspect.313;
let Inspect.311 : Str = CallByName Inspect.251 Inspect.312 Inspect.316;
ret Inspect.311;

procedure Inspect.61 (Inspect.303, Inspect.298):
let Inspect.321 : Str = CallByName Str.3 Inspect.303 Inspect.298;
dec Inspect.298;
ret Inspect.321;
procedure Inspect.61 (Inspect.304, Inspect.300):
let Inspect.325 : Str = CallByName Str.3 Inspect.304 Inspect.300;
dec Inspect.300;
ret Inspect.325;

procedure Inspect.62 (Inspect.306):
ret Inspect.306;

procedure Str.3 (#Attr.2, #Attr.3):
let Str.292 : Str = lowlevel StrConcat #Attr.2 #Attr.3;
ret Str.292;

procedure Test.0 ():
let Test.4 : Str = "";
let Test.3 : Str = CallByName Inspect.5 Test.4;
let Test.1 : Str = CallByName Inspect.35 Test.3;
let Test.3 : Str = "";
let Test.1 : Str = CallByName Inspect.34 Test.3;
dbg Test.1;
dec Test.1;
let Test.2 : I64 = 42i64;
Expand Down
Loading

0 comments on commit a187d14

Please sign in to comment.