-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Implement array inference #524
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
Changes from all commits
4729a1b
d665acb
2a3262c
5d3884d
478ce1c
5c570fa
f66ca1b
0aedd4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,6 +334,32 @@ fn test(x: &str, y: isize) { | |
); | ||
} | ||
|
||
#[test] | ||
fn infer_array() { | ||
check_inference( | ||
r#" | ||
fn test(x: &str, y: isize) { | ||
let a = [x]; | ||
let b = [a, a]; | ||
let c = [b, b]; | ||
|
||
let d = [y, 1, 2, 3]; | ||
let d = [1, y, 2, 3]; | ||
let e = [y]; | ||
let f = [d, d]; | ||
let g = [e, e]; | ||
|
||
let h = [1, 2]; | ||
let i = ["a", "b"]; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here's an expression that checks whether the element types are actually unified:
the 1, 2 and 3 should be inferred to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could also add the case where the array is empty:
we should still infer the element type on the right side. I think this should already work. This would also test array type references, which you implemented but didn't use in this test (the |
||
let b = [a, ["b"]]; | ||
let x: [u8; 0] = []; | ||
} | ||
"#, | ||
"array.txt", | ||
); | ||
} | ||
|
||
fn infer(content: &str) -> String { | ||
let (db, _, file_id) = MockDatabase::with_single_file(content); | ||
let source_file = db.source_file(file_id); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
[9; 10) 'x': &str | ||
[18; 19) 'y': isize | ||
[28; 293) '{ ... []; }': () | ||
[38; 39) 'a': [&str] | ||
[42; 45) '[x]': [&str] | ||
[43; 44) 'x': &str | ||
[55; 56) 'b': [[&str]] | ||
[59; 65) '[a, a]': [[&str]] | ||
[60; 61) 'a': [&str] | ||
[63; 64) 'a': [&str] | ||
[75; 76) 'c': [[[&str]]] | ||
[79; 85) '[b, b]': [[[&str]]] | ||
[80; 81) 'b': [[&str]] | ||
[83; 84) 'b': [[&str]] | ||
[96; 97) 'd': [isize] | ||
[100; 112) '[y, 1, 2, 3]': [isize] | ||
[101; 102) 'y': isize | ||
[104; 105) '1': isize | ||
[107; 108) '2': isize | ||
[110; 111) '3': isize | ||
[122; 123) 'd': [isize] | ||
[126; 138) '[1, y, 2, 3]': [isize] | ||
[127; 128) '1': isize | ||
[130; 131) 'y': isize | ||
[133; 134) '2': isize | ||
[136; 137) '3': isize | ||
[148; 149) 'e': [isize] | ||
[152; 155) '[y]': [isize] | ||
[153; 154) 'y': isize | ||
[165; 166) 'f': [[isize]] | ||
[169; 175) '[d, d]': [[isize]] | ||
[170; 171) 'd': [isize] | ||
[173; 174) 'd': [isize] | ||
[185; 186) 'g': [[isize]] | ||
[189; 195) '[e, e]': [[isize]] | ||
[190; 191) 'e': [isize] | ||
[193; 194) 'e': [isize] | ||
[206; 207) 'h': [i32] | ||
[210; 216) '[1, 2]': [i32] | ||
[211; 212) '1': i32 | ||
[214; 215) '2': i32 | ||
[226; 227) 'i': [&str] | ||
[230; 240) '["a", "b"]': [&str] | ||
[231; 234) '"a"': &str | ||
[236; 239) '"b"': &str | ||
[251; 252) 'b': [[&str]] | ||
[255; 265) '[a, ["b"]]': [[&str]] | ||
[256; 257) 'a': [&str] | ||
[259; 264) '["b"]': [&str] | ||
[260; 263) '"b"': &str | ||
[275; 276) 'x': [u8] | ||
[288; 290) '[]': [u8] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we define
Array(Ty, ty::Const)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't currently have a
ty::Const
(the commented-out lines are copied from rustc) :) And for our current use cases, we don't really need the array size -- if there was a mismatch in sizes somewhere, I think we would probably want to continue type inference anyway to still be able to provide completions etc.