Skip to content

Commit

Permalink
Improve formatting of get_id types
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Feb 7, 2024
1 parent 6416283 commit 7c5bf6c
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 66 deletions.
13 changes: 8 additions & 5 deletions src/cli/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hakana_reflection_info::Interner;
use indexmap::IndexMap;
use rand::Rng;
use rustc_hash::FxHashSet;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::fs::{self, File};
use std::io::Write;
Expand Down Expand Up @@ -1056,22 +1056,25 @@ fn do_codegen(
errors.push(format!("File {} doesn’t exist", name));
continue;
}
} else if check_codegen || !overwrite_codegen {
} else {
let existing_contents = fs::read_to_string(path).unwrap();
if existing_contents.trim() != info.trim() {
errors.push(format!("File {} differs from codegen", name,));
if check_codegen || !overwrite_codegen {
errors.push(format!("File {} differs from codegen", name));
continue;
}
} else {
verified_count += 1;
continue;
}

continue;
}

if let Some(dir) = path.parent() {
fs::create_dir_all(dir).unwrap();
}
let mut output_path = fs::File::create(path).unwrap();
write!(output_path, "{}", &info).unwrap();
println!("Saved {}", name);
updated_count += 1;
}

Expand Down
87 changes: 63 additions & 24 deletions src/code_info/t_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,33 @@ pub enum TAtomic {

impl TAtomic {
pub fn get_id(&self, interner: Option<&Interner>) -> String {
self.get_id_with_refs(interner, &mut vec![])
self.get_id_with_refs(interner, &mut vec![], None)
}

pub fn get_id_with_refs(&self, interner: Option<&Interner>, refs: &mut Vec<StrId>) -> String {
pub fn get_id_with_refs(
&self,
interner: Option<&Interner>,
refs: &mut Vec<StrId>,
indent: Option<usize>,
) -> String {
match self {
TAtomic::TArraykey { .. } => "arraykey".to_string(),
TAtomic::TBool { .. } => "bool".to_string(),
TAtomic::TClassname { as_type, .. } => {
let mut str = String::new();
str += "classname<";
str += as_type.get_id_with_refs(interner, &mut vec![]).as_str();
str += as_type
.get_id_with_refs(interner, &mut vec![], indent)
.as_str();
str += ">";
str
}
TAtomic::TTypename { as_type, .. } => {
let mut str = String::new();
str += "typename<";
str += as_type.get_id_with_refs(interner, &mut vec![]).as_str();
str += as_type
.get_id_with_refs(interner, &mut vec![], indent)
.as_str();
str += ">";
str
}
Expand Down Expand Up @@ -215,36 +224,64 @@ impl TAtomic {
if let Some(known_items) = known_items {
str += "shape(";

if let Some(indent) = indent {
str += "\n";
str += &("\t".repeat(indent + 1));
}

str += known_items
.iter()
.map(|(property, (u, property_type))| {
format!(
"{}{} => {}",
if *u { "?" } else { "" },
property.to_string(interner),
property_type.get_id_with_refs(interner, refs)
property_type.get_id_with_refs(
interner,
refs,
if let Some(indent) = indent {
Some(indent + 1)
} else {
None
}
)
)
})
.join(", ")
.join(&if let Some(indent) = indent {
format!("\n{}", "\t".repeat(indent + 1))
} else {
", ".to_string()
})
.as_str();

if let Some(params) = params {
str += ", ...dict<";
str += params.0.get_id_with_refs(interner, refs).as_str();
str += ",";
str += params.1.get_id_with_refs(interner, refs).as_str();
if let Some(indent) = indent {
str += &format!("\n{}", "\t".repeat(indent + 1));
} else {
str += ", "
}

str += "...dict<";
str += params.0.get_id_with_refs(interner, refs, indent).as_str();
str += ", ";
str += params.1.get_id_with_refs(interner, refs, indent).as_str();
str += ">";
}

if let Some(indent) = indent {
str += "\n";
str += &("\t".repeat(indent));
}

str += ")";
return str;
}

if let Some(params) = params {
str += "dict<";
str += params.0.get_id_with_refs(interner, refs).as_str();
str += ",";
str += params.1.get_id_with_refs(interner, refs).as_str();
str += params.0.get_id_with_refs(interner, refs, indent).as_str();
str += ", ";
str += params.1.get_id_with_refs(interner, refs, indent).as_str();
str += ">";
str
} else {
Expand Down Expand Up @@ -275,7 +312,7 @@ impl TAtomic {
format!(
"{}{}",
if let Some(param_type) = &param.signature_type {
param_type.get_id_with_refs(interner, refs)
param_type.get_id_with_refs(interner, refs, indent)
} else {
"mixed".to_string()
},
Expand All @@ -287,7 +324,9 @@ impl TAtomic {

str += "): ";
if let Some(return_type) = return_type {
str += return_type.get_id_with_refs(interner, refs).as_str();
str += return_type
.get_id_with_refs(interner, refs, indent)
.as_str();
} else {
str += "mixed";
}
Expand All @@ -311,7 +350,7 @@ impl TAtomic {
TAtomic::TKeyset { type_param, .. } => {
let mut str = String::new();
str += "keyset<";
str += type_param.get_id_with_refs(interner, refs).as_str();
str += type_param.get_id_with_refs(interner, refs, indent).as_str();
str += ">";
str
}
Expand Down Expand Up @@ -403,7 +442,7 @@ impl TAtomic {
"&".to_string()
+ extra_types
.iter()
.map(|atomic| atomic.get_id_with_refs(interner, refs))
.map(|atomic| atomic.get_id_with_refs(interner, refs, indent))
.join("&")
.as_str()
} else {
Expand All @@ -423,7 +462,7 @@ impl TAtomic {
str += "<";
str += type_params
.iter()
.map(|tunion| tunion.get_id_with_refs(interner, refs))
.map(|tunion| tunion.get_id_with_refs(interner, refs, indent))
.join(", ")
.as_str();
str += ">";
Expand Down Expand Up @@ -457,7 +496,7 @@ impl TAtomic {
str += "<";
str += type_params
.iter()
.map(|tunion| tunion.get_id_with_refs(interner, refs))
.map(|tunion| tunion.get_id_with_refs(interner, refs, indent))
.join(", ")
.as_str();
str += ">)";
Expand Down Expand Up @@ -558,13 +597,13 @@ impl TAtomic {
str += "tuple(";
str += known_items
.iter()
.map(|(_, (_, tunion))| tunion.get_id_with_refs(interner, refs))
.map(|(_, (_, tunion))| tunion.get_id_with_refs(interner, refs, indent))
.join(", ")
.as_str();

if !type_param.is_nothing() {
str += ", ...vec<";
str += type_param.get_id_with_refs(interner, refs).as_str();
str += type_param.get_id_with_refs(interner, refs, indent).as_str();
str += ">";
}

Expand All @@ -573,7 +612,7 @@ impl TAtomic {
}
let mut str = String::new();
str += if *non_empty { "non-empty-vec<" } else { "vec<" };
str += type_param.get_id_with_refs(interner, refs).as_str();
str += type_param.get_id_with_refs(interner, refs, indent).as_str();
str += ">";
str
}
Expand All @@ -597,7 +636,7 @@ impl TAtomic {
} => {
format!(
"{}::{}",
class_type.get_id_with_refs(interner, refs),
class_type.get_id_with_refs(interner, refs, indent),
if let Some(interner) = interner {
interner.lookup(member_name).to_string()
} else {
Expand Down Expand Up @@ -676,7 +715,7 @@ impl TAtomic {
| TAtomic::TBool { .. }
| TAtomic::TEnumClassLabel { .. }
| TAtomic::TMixedWithFlags(..)
| TAtomic::TTypeVariable { .. } => self.get_id_with_refs(None, &mut vec![]),
| TAtomic::TTypeVariable { .. } => self.get_id_with_refs(None, &mut vec![], None),

TAtomic::TStringWithFlags(..) => "string".to_string(),

Expand Down
38 changes: 29 additions & 9 deletions src/code_info/t_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,35 @@ impl TUnion {
}

pub fn get_id(&self, interner: Option<&Interner>) -> String {
self.get_id_with_refs(interner, &mut vec![])
}

pub fn get_id_with_refs(&self, interner: Option<&Interner>, refs: &mut Vec<StrId>) -> String {
let mut tatomic_strings = self
.types
.iter()
.map(|atomic| atomic.get_id_with_refs(interner, refs));
tatomic_strings.join("|")
self.get_id_with_refs(interner, &mut vec![], None)
}

pub fn get_id_with_refs(
&self,
interner: Option<&Interner>,
refs: &mut Vec<StrId>,
indent: Option<usize>,
) -> String {
if self.types.len() == 2 {
match (&self.types[0], &self.types[1]) {
(TAtomic::TNull, a) | (a, TAtomic::TNull) => {
format!("?{}", a.get_id_with_refs(interner, refs, indent))
}
(a, b) => {
format!(
"{}|{}",
a.get_id_with_refs(interner, refs, indent),
b.get_id_with_refs(interner, refs, indent)
)
}
}
} else {
let mut tatomic_strings = self
.types
.iter()
.map(|atomic| atomic.get_id_with_refs(interner, refs, indent));
tatomic_strings.join("|")
}
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion tests/diff/classConstTypeChanged/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:13:14 - Argument 1 of echo expects scalar|null, different type Exception provided
ERROR: InvalidArgument - input.hack:13:14 - Argument 1 of echo expects ?scalar, different type Exception provided
4 changes: 2 additions & 2 deletions tests/diff/commentOutClassInOtherFile/output.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ERROR: NonExistentClass - input.hack:8:11 - Cannot call new on undefined class A
ERROR: NonExistentType - input.hack:12:10 - Unknown class A
ERROR: MixedAnyArgument - input.hack:12:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: MixedAnyArgument - input.hack:12:10 - Argument 1 of echo expects ?scalar, any provided
ERROR: NonExistentType - input.hack:17:14 - Unknown class A
ERROR: MixedAnyArgument - input.hack:17:14 - Argument 1 of echo expects scalar|null, any provided
ERROR: MixedAnyArgument - input.hack:17:14 - Argument 1 of echo expects ?scalar, any provided
ERROR: NonExistentClass - input.hack:21:15 - Cannot call new on undefined class A
ERROR: InvalidReturnStatement - input.hack:26:16 - The type string(a) does not match the declared return type int for B::bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ERROR: PossiblyUndefinedStringArrayOffset - input.hack:6:10 - Fetch on dict<arraykey,any> using possibly-undefined key 'foo'
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: PossiblyUndefinedStringArrayOffset - input.hack:6:10 - Fetch on dict<arraykey, any> using possibly-undefined key 'foo'
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects ?scalar, any provided
Original file line number Diff line number Diff line change
@@ -1 +1 @@
InvalidReturnStatement - input.hack:9:12 - The type dict<string,keyset<int>> does not match the declared return type dict<string,keyset<string>> for foo
InvalidReturnStatement - input.hack:9:12 - The type dict<string, keyset<int>> does not match the declared return type dict<string, keyset<string>> for foo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ERROR: ImpossibleKeyCheck - input.hack:7:9 - Type vec<string> never has key 's'
ERROR: ImpossibleKeyCheck - input.hack:11:9 - Type vec<string> never has key 't'
ERROR: ImpossibleKeyCheck - input.hack:24:9 - Type dict<string,string> never has key 1
ERROR: ImpossibleKeyCheck - input.hack:28:9 - Type dict<string,string> never has key 2
ERROR: ImpossibleKeyCheck - input.hack:24:9 - Type dict<string, string> never has key 1
ERROR: ImpossibleKeyCheck - input.hack:28:9 - Type dict<string, string> never has key 2
2 changes: 1 addition & 1 deletion tests/inference/DictVecKeyset/dictFromKeys/output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ERROR: LessSpecificNestedAnyReturnStatement - input.hack:2:12 - The type dict<arraykey,string> is more general than the declared return type dict<string,string> for foo
ERROR: LessSpecificNestedAnyReturnStatement - input.hack:2:12 - The type dict<arraykey, string> is more general than the declared return type dict<string, string> for foo
ERROR: MixedArgument - input.hack:3:9 - Argument 1 of HH\Lib\Dict\from_keys expects HH\Traversable<arraykey>, mixed provided
ERROR: LessSpecificNestedAnyArgumentType - input.hack:4:28 - Argument 1 of takesString expects string, parent type arraykey provided
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ERROR: LessSpecificNestedAnyArgumentType - input.hack:6:14 - Argument 1 of foo1 expects shape('id' => string, 'name' => string), parent type dict<arraykey,any> provided
ERROR: LessSpecificNestedAnyArgumentType - input.hack:7:14 - Argument 1 of foo2 expects shape('id' => string, 'name' => string, ...dict<arraykey,any>), parent type dict<arraykey,any> provided
ERROR: LessSpecificNestedArgumentType - input.hack:12:10 - Argument 1 of foo1 expects shape('id' => string, 'name' => string), parent type dict<string,mixed> provided
ERROR: LessSpecificNestedArgumentType - input.hack:13:10 - Argument 1 of foo2 expects shape('id' => string, 'name' => string, ...dict<arraykey,any>), parent type dict<string,mixed> provided
ERROR: LessSpecificNestedAnyArgumentType - input.hack:6:14 - Argument 1 of foo1 expects shape('id' => string, 'name' => string), parent type dict<arraykey, any> provided
ERROR: LessSpecificNestedAnyArgumentType - input.hack:7:14 - Argument 1 of foo2 expects shape('id' => string, 'name' => string, ...dict<arraykey, any>), parent type dict<arraykey, any> provided
ERROR: LessSpecificNestedArgumentType - input.hack:12:10 - Argument 1 of foo1 expects shape('id' => string, 'name' => string), parent type dict<string, mixed> provided
ERROR: LessSpecificNestedArgumentType - input.hack:13:10 - Argument 1 of foo2 expects shape('id' => string, 'name' => string, ...dict<arraykey, any>), parent type dict<string, mixed> provided
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ERROR: MixedAnyArgument - input.hack:4:29 - Argument 1 of HH\vec expects HH\Traversable<any>, any provided
ERROR: LessSpecificNestedAnyArgumentType - input.hack:6:9 - Argument 1 of foo expects shape('a' => null|vec<shape('b' => string)>), parent type shape('a' => vec<any>|null) provided
ERROR: LessSpecificNestedAnyArgumentType - input.hack:6:9 - Argument 1 of foo expects shape('a' => ?vec<shape('b' => string)>), parent type shape('a' => ?vec<any>) provided
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:21:10 - Argument 1 of echo expects scalar|null, different type Node provided
ERROR: InvalidArgument - input.hack:21:10 - Argument 1 of echo expects ?scalar, different type Node provided
2 changes: 1 addition & 1 deletion tests/inference/TypeAlias/classConstTypeAlias/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:11:14 - Argument 1 of echo expects scalar|null, different type Exception provided
ERROR: InvalidArgument - input.hack:11:14 - Argument 1 of echo expects ?scalar, different type Exception provided
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:23:14 - Argument 1 of echo expects scalar|null, different type Exception provided
ERROR: InvalidArgument - input.hack:23:14 - Argument 1 of echo expects ?scalar, different type Exception provided
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ERROR: MixedAnyArrayAccess - input.hack:3:10 - Unsafe array access on value with type any
ERROR: MixedAnyArrayAccess - input.hack:4:10 - Unsafe array access on value with type nonnull-from-any
ERROR: MixedAnyArgument - input.hack:4:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: MixedAnyArgument - input.hack:4:10 - Argument 1 of echo expects ?scalar, any provided
ERROR: MixedAnyReturnStatement - input.hack:5:12 - Could not infer a proper return type — saw nonnull-from-any
Loading

0 comments on commit 7c5bf6c

Please sign in to comment.