Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Inspect to builtins #5747

Merged
merged 26 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5cbc389
add userland imple of inspect with some apps
bhansconnect Jun 22, 2023
7bf3dd2
add set function and make all collection functions generic
bhansconnect Jun 23, 2023
2e7343e
change to community instead of person
bhansconnect Jun 23, 2023
17f68aa
fix pretty printing
bhansconnect Jun 23, 2023
5379aef
add opaque type support to formatters
bhansconnect Jul 5, 2023
77e8b13
switch LogFormatter to use a Str as the base type
bhansconnect Jul 5, 2023
b983010
Add type annotation to Inspect.custom
rtfeldman Aug 11, 2023
390b5fe
Rename Formatter to InspectFormatter
rtfeldman Aug 11, 2023
24806fa
Rename Inspect WalkFn aliases
rtfeldman Aug 11, 2023
75f6293
Merge remote-tracking branch 'origin/main' into inspect-builtin
rtfeldman Aug 11, 2023
700776f
Make Inspect a builtin
rtfeldman Aug 11, 2023
545c071
Fix a test
rtfeldman Aug 11, 2023
6b9592a
Update some insta tests
rtfeldman Aug 11, 2023
08e7b52
Update mono tests
rtfeldman Aug 11, 2023
9b8d8e8
Fix some formatting on Community.roc
rtfeldman Aug 11, 2023
60bbd33
Work around `roc format` bug
rtfeldman Aug 11, 2023
04739fb
roc format
rtfeldman Aug 11, 2023
f682729
Update uitest
rtfeldman Aug 11, 2023
5d6c787
Special-case layout conversions for builtin alias
rtfeldman Aug 14, 2023
15a6bc3
Merge remote-tracking branch 'origin/main' into inspect-builtin
rtfeldman Aug 14, 2023
83ead6b
Special-case TotallyNotJson types for now
rtfeldman Aug 14, 2023
995c985
Give up on exhaustively enumerating aliases
rtfeldman Aug 14, 2023
e8e1d04
Update mono tests
rtfeldman Aug 15, 2023
0646138
added comments, tests
Anton-4 Aug 16, 2023
63b7fef
Merge + update mono tests
Anton-4 Aug 16, 2023
ba8b7f6
roc fmt on inspect examples
rtfeldman Aug 17, 2023
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
27 changes: 25 additions & 2 deletions crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ mod cli_run {

if !actual.ends_with(expected_ending) {
panic!(
"expected output to end with:\n{}\nbut instead got:\n{}\n stderr was:\n{}",
"> expected output to end with:\n{}\n> but instead got:\n{}\n> stderr was:\n{}",
expected_ending, actual, out.stderr
);
}
Expand Down Expand Up @@ -387,7 +387,7 @@ mod cli_run {
let mut custom_flags: Vec<&str> = Vec::new();

match executable_filename {
"form" | "hello-gui" | "breakout" | "libhello" => {
"form" | "hello-gui" | "breakout" | "libhello" | "inspect-gui" => {
// Since these require things the build system often doesn't have
// (e.g. GUIs open a window, Ruby needs ruby installed, WASM needs a browser)
// we do `roc build` on them but don't run them.
Expand Down Expand Up @@ -938,6 +938,29 @@ mod cli_run {
test_roc_expect("examples/parser/package", "ParserHttp.roc")
}

#[test]
fn inspect_logging() {
test_roc_app_slim(
"examples",
"inspect-logging.roc",
"inspect-logging",
r#"{people: [{firstName: "John", lastName: "Smith", age: 27, hasBeard: true, favoriteColor: Blue}, {firstName: "Debby", lastName: "Johnson", age: 47, hasBeard: false, favoriteColor: Green}, {firstName: "Jane", lastName: "Doe", age: 33, hasBeard: false, favoriteColor: (RGB (255, 255, 0))}], friends: [{2}, {2}, {0, 1}]}
"#,
UseValgrind::Yes,
)
}

#[test]
fn inspect_gui() {
test_roc_app_slim(
"examples",
"inspect-gui.roc",
"inspect-gui",
"",
UseValgrind::No,
)
}

// TODO not sure if this cfg should still be here: #[cfg(not(debug_assertions))]
// this is for testing the benchmarks, to perform proper benchmarks see crates/cli/benches/README.md
mod test_benchmarks {
Expand Down
94 changes: 94 additions & 0 deletions crates/compiler/builtins/roc/Inspect.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
interface Inspect
exposes [
Inspect,
Inspector,
InspectFormatter,
ElemWalker,
KeyValWalker,
inspect,
init,
list,
set,
dict,
tag,
tuple,
record,
bool,
str,
opaque,
u8,
i8,
u16,
i16,
u32,
i32,
u64,
i64,
u128,
i128,
f32,
f64,
dec,
custom,
apply,
toInspector,
]
imports [
Bool.{ Bool },
Num.{ U8, U16, U32, U64, U128, I8, I16, I32, I64, I128, F32, F64, Dec },
List,
Str,
]

KeyValWalker state collection key val : collection, state, (state, key, val -> state) -> state
ElemWalker state collection elem : collection, state, (state, elem -> state) -> state

InspectFormatter implements
init : {} -> f where f implements InspectFormatter

tag : Str, List (Inspector f) -> Inspector f where f implements InspectFormatter
tuple : List (Inspector f) -> Inspector f where f implements InspectFormatter
record : List { key : Str, value : Inspector f } -> Inspector f where f implements InspectFormatter
bool : Bool -> Inspector f where f implements InspectFormatter
str : Str -> Inspector f where f implements InspectFormatter

list : list, ElemWalker state list elem, (elem -> Inspector f) -> Inspector f where f implements InspectFormatter
set : set, ElemWalker state set elem, (elem -> Inspector f) -> Inspector f where f implements InspectFormatter
dict : dict, KeyValWalker state dict key value, (key -> Inspector f), (value -> Inspector f) -> Inspector f where f implements InspectFormatter

# Note opaque is used for both opaque types and functions.
# The auto deriver for functions probably could put the function type.
# For regular opaque types, I think we can use the type name, though that may lead to some reflection related issues that still need to be discussed.
# As a simple baseline, it can just use the exact words `opaque` and `function` for now.
# In text, this would render as `<opaque>`, `<function>`, etc
opaque : Str -> Inspector f where f implements InspectFormatter

u8 : U8 -> Inspector f where f implements InspectFormatter
i8 : I8 -> Inspector f where f implements InspectFormatter
u16 : U16 -> Inspector f where f implements InspectFormatter
i16 : I16 -> Inspector f where f implements InspectFormatter
u32 : U32 -> Inspector f where f implements InspectFormatter
i32 : I32 -> Inspector f where f implements InspectFormatter
u64 : U64 -> Inspector f where f implements InspectFormatter
i64 : I64 -> Inspector f where f implements InspectFormatter
u128 : U128 -> Inspector f where f implements InspectFormatter
i128 : I128 -> Inspector f where f implements InspectFormatter
f32 : F32 -> Inspector f where f implements InspectFormatter
f64 : F64 -> Inspector f where f implements InspectFormatter
dec : Dec -> Inspector f where f implements InspectFormatter

Inspector f := f -> f where f implements InspectFormatter

custom : (f -> f) -> Inspector f where f implements InspectFormatter
custom = @Inspector

apply : Inspector f, f -> f where f implements InspectFormatter
apply = \@Inspector fn, fmt -> fn fmt

Inspect implements
toInspector : val -> Inspector f where val implements Inspect, f implements InspectFormatter

inspect : val -> f where val implements Inspect, f implements InspectFormatter
inspect = \val ->
(@Inspector valFn) = toInspector val
valFn (init {})
2 changes: 2 additions & 0 deletions crates/compiler/builtins/src/roc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn module_source(module_id: ModuleId) -> &'static str {
ModuleId::ENCODE => ENCODE,
ModuleId::DECODE => DECODE,
ModuleId::HASH => HASH,
ModuleId::INSPECT => INSPECT,
ModuleId::JSON => JSON,
_ => internal_error!(
"ModuleId {:?} is not part of the standard library",
Expand All @@ -34,4 +35,5 @@ const BOOL: &str = include_str!("../roc/Bool.roc");
const ENCODE: &str = include_str!("../roc/Encode.roc");
const DECODE: &str = include_str!("../roc/Decode.roc");
const HASH: &str = include_str!("../roc/Hash.roc");
const INSPECT: &str = include_str!("../roc/Inspect.roc");
const JSON: &str = include_str!("../roc/TotallyNotJson.roc");
1 change: 1 addition & 0 deletions crates/compiler/load/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const MODULES: &[(ModuleId, &str)] = &[
(ModuleId::ENCODE, "Encode.roc"),
(ModuleId::DECODE, "Decode.roc"),
(ModuleId::HASH, "Hash.roc"),
(ModuleId::INSPECT, "Inspect.roc"),
(ModuleId::JSON, "TotallyNotJson.roc"),
];

Expand Down
2 changes: 2 additions & 0 deletions crates/compiler/load/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const BOX: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Box.dat")) as &[_];
const ENCODE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Encode.dat")) as &[_];
const DECODE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Decode.dat")) as &[_];
const HASH: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Hash.dat")) as &[_];
const INSPECT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Inspect.dat")) as &[_];

fn deserialize_help(bytes: &[u8]) -> TypeState {
let (state, _offset) = TypeState::deserialize(bytes);
Expand Down Expand Up @@ -242,6 +243,7 @@ fn read_cached_types() -> MutMap<ModuleId, TypeState> {
output.insert(ModuleId::DECODE, deserialize_help(DECODE));

output.insert(ModuleId::HASH, deserialize_help(HASH));
output.insert(ModuleId::INSPECT, deserialize_help(INSPECT));
}

output
Expand Down
3 changes: 3 additions & 0 deletions crates/compiler/load_internal/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,7 @@ fn update<'a>(
extend_header_with_builtin(header, ModuleId::ENCODE);
extend_header_with_builtin(header, ModuleId::DECODE);
extend_header_with_builtin(header, ModuleId::HASH);
extend_header_with_builtin(header, ModuleId::INSPECT);
}

state
Expand Down Expand Up @@ -3494,6 +3495,7 @@ fn load_module<'a>(
"Encode", ModuleId::ENCODE
"Decode", ModuleId::DECODE
"Hash", ModuleId::HASH
"Inspect", ModuleId::INSPECT
"TotallyNotJson", ModuleId::JSON
}

Expand Down Expand Up @@ -5257,6 +5259,7 @@ fn canonicalize_and_constrain<'a>(
| ModuleId::DICT
| ModuleId::SET
| ModuleId::HASH
| ModuleId::INSPECT
);

if !name.is_builtin() || should_include_builtin {
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/load_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ pub const BUILTIN_MODULES: &[(ModuleId, &str)] = &[
(ModuleId::ENCODE, "Encode"),
(ModuleId::DECODE, "Decode"),
(ModuleId::HASH, "Hash"),
(ModuleId::INSPECT, "Inspect"),
(ModuleId::JSON, "TotallyNotJson"),
];
1 change: 1 addition & 0 deletions crates/compiler/load_internal/src/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl Default for ModuleCache<'_> {
ENCODE,
DECODE,
HASH,
INSPECT,
JSON,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/load_internal/tests/test_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,8 @@ fn issue_2863_module_type_does_not_exist() {
Did you mean one of these?

Decoding
Result
Dict
Result
DecodeError
"
)
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/module/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl ModuleName {
pub const ENCODE: &'static str = "Encode";
pub const DECODE: &'static str = "Decode";
pub const HASH: &'static str = "Hash";
pub const INSPECT: &'static str = "Inspect";
pub const JSON: &'static str = "TotallyNotJson";

pub fn as_str(&self) -> &str {
Expand Down
44 changes: 42 additions & 2 deletions crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,9 +1588,49 @@ define_builtins! {
20 HASH_HASH_LIST: "hashList"
21 HASH_HASH_UNORDERED: "hashUnordered"
}
14 JSON: "TotallyNotJson" => {
14 INSPECT: "Inspect" => {
0 INSPECT_INSPECT_ABILITY: "Inspect" exposed_type=true
1 INSPECT_INSPECTOR: "Inspector" exposed_type=true
2 INSPECT_INSPECT_FORMATTER: "InspectFormatter" exposed_type=true
3 INSPECT_ELEM_WALKER: "ElemWalker" exposed_type=true
4 INSPECT_KEY_VAL_WALKER: "KeyValWalker" exposed_type=true
5 INSPECT_INSPECT: "inspect"
6 INSPECT_INIT: "init"
7 INSPECT_LIST: "list"
8 INSPECT_SET: "set"
9 INSPECT_DICT: "dict"
10 INSPECT_TAG: "tag"
11 INSPECT_TUPLE: "tuple"
12 INSPECT_RECORD: "record"
13 INSPECT_BOOL: "bool"
14 INSPECT_STR: "str"
15 INSPECT_OPAQUE: "opaque"
16 INSPECT_U8: "u8"
17 INSPECT_I8: "i8"
18 INSPECT_U16: "u16"
19 INSPECT_I16: "i16"
20 INSPECT_U32: "u32"
21 INSPECT_I32: "i32"
22 INSPECT_U64: "u64"
23 INSPECT_I64: "i64"
24 INSPECT_U128: "u128"
25 INSPECT_I128: "i128"
26 INSPECT_F32: "f32"
27 INSPECT_F64: "f64"
28 INSPECT_DEC: "dec"
29 INSPECT_CUSTOM: "custom"
30 INSPECT_APPLY: "apply"
31 INSPECT_TO_INSPECTOR: "toInspector"
}
15 JSON: "TotallyNotJson" => {
0 JSON_JSON: "TotallyNotJson"
1 JSON_FIELD_NAME_MAPPING: "FieldNameMapping"
2 JSON_NUMBER_STATE: "NumberState"
3 JSON_STRING_STATE: "StringState"
4 JSON_ARRAY_OPENING_STATE: "ArrayOpeningState"
5 JSON_ARRAY_CLOSING_STATE: "ArrayClosingState"
6 JSON_OBJECT_STATE: "ObjectState"
}

num_modules: 15 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro)
num_modules: 16 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro)
}
2 changes: 1 addition & 1 deletion crates/compiler/mono/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3965,7 +3965,7 @@ fn build_specialized_proc<'a>(
}
}
Ordering::Less => panic!(
"more argument symbols than arguments (according to the layout) for {proc_name:?}"
"more argument symbols than arguments (according to the layout) for {proc_name:?}. Pattern symbols: {:?}\n\nPattern layouts: {:?}", pattern_symbols, pattern_layouts_len,
),
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/compiler/mono/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,9 @@ impl<'a> RawFunctionLayout<'a> {
cacheable(Ok(Self::ZeroArgumentThunk(Layout::usize(env.target_info))))
}

Alias(symbol, _, _, _) if symbol.is_builtin() => {
Alias(Symbol::INSPECT_ELEM_WALKER | Symbol::INSPECT_KEY_VAL_WALKER, _, var, _) => Self::from_var(env, var),

Alias(symbol, _, var, _) if symbol.is_builtin() => {
Layout::new_help(env, var, content).then(Self::ZeroArgumentThunk)
}

Expand Down
14 changes: 7 additions & 7 deletions crates/compiler/test_mono/generated/capture_void_layout_task.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ procedure Num.51 (#Attr.2, #Attr.3):

procedure Test.10 (Test.66, #Attr.12):
let Test.9 : {} = UnionAtIndex (Id 0) (Index 0) #Attr.12;
let #Derived_gen.18 : Int1 = lowlevel RefCountIsUnique #Attr.12;
if #Derived_gen.18 then
let #Derived_gen.20 : Int1 = lowlevel RefCountIsUnique #Attr.12;
if #Derived_gen.20 then
free #Attr.12;
ret Test.9;
else
Expand All @@ -60,7 +60,7 @@ procedure Test.10 (Test.66, #Attr.12):
procedure Test.14 (Test.45, #Attr.12):
let Test.13 : {{}, []} = UnionAtIndex (Id 1) (Index 1) #Attr.12;
let Test.12 : [<r>C {}, C *self {{}, []}] = UnionAtIndex (Id 1) (Index 0) #Attr.12;
joinpoint #Derived_gen.19:
joinpoint #Derived_gen.18:
let Test.50 : {} = Struct {};
let Test.51 : U8 = GetTagId Test.12;
joinpoint Test.52 Test.15:
Expand All @@ -87,14 +87,14 @@ procedure Test.14 (Test.45, #Attr.12):
jump Test.52 Test.53;

in
let #Derived_gen.20 : Int1 = lowlevel RefCountIsUnique #Attr.12;
if #Derived_gen.20 then
let #Derived_gen.19 : Int1 = lowlevel RefCountIsUnique #Attr.12;
if #Derived_gen.19 then
free #Attr.12;
jump #Derived_gen.19;
jump #Derived_gen.18;
else
inc Test.12;
decref #Attr.12;
jump #Derived_gen.19;
jump #Derived_gen.18;

procedure Test.20 (Test.21, Test.18):
let Test.23 : [C {}, C []] = CallByName Test.32 Test.21 Test.18;
Expand Down
Loading
Loading