7020: Implement const pat and expr inference r=flodiebold a=Veykril



Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2020-12-23 19:50:04 +00:00 committed by GitHub
commit a82c2445be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 76 additions and 11 deletions

View file

@ -155,7 +155,7 @@ impl<'a> InferenceContext<'a> {
}
None => self.infer_block(statements, *tail, expected),
},
Expr::Unsafe { body } => self.infer_expr(*body, expected),
Expr::Unsafe { body } | Expr::Const { body } => self.infer_expr(*body, expected),
Expr::TryBlock { body } => {
let _inner = self.infer_expr(*body, expected);
// FIXME should be std::result::Result<{inner}, _>

View file

@ -243,6 +243,9 @@ impl<'a> InferenceContext<'a> {
}
None => Ty::Unknown,
},
Pat::ConstBlock(expr) => {
self.infer_expr(*expr, &Expectation::has_type(expected.clone()))
}
Pat::Missing => Ty::Unknown,
};
// use a new type variable if we got Ty::Unknown here
@ -264,8 +267,9 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
| Pat::Range { .. }
| Pat::Slice { .. } => true,
Pat::Or(pats) => pats.iter().all(|p| is_non_ref_pat(body, *p)),
// FIXME: Path/Lit might actually evaluate to ref, but inference is unimplemented.
// FIXME: ConstBlock/Path/Lit might actually evaluate to ref, but inference is unimplemented.
Pat::Path(..) => true,
Pat::ConstBlock(..) => true,
Pat::Lit(expr) => match body[*expr] {
Expr::Literal(Literal::String(..)) => false,
_ => true,

View file

@ -774,3 +774,33 @@ fn foo(tuple: Tuple) {
"#]],
);
}
#[test]
fn const_block_pattern() {
check_infer(
r#"
struct Foo(usize);
fn foo(foo: Foo) {
match foo {
const { Foo(15 + 32) } => {},
_ => {}
}
}"#,
expect![[r#"
26..29 'foo': Foo
36..115 '{ ... } }': ()
42..113 'match ... }': ()
48..51 'foo': Foo
62..84 'const ... 32) }': Foo
68..84 '{ Foo(... 32) }': Foo
70..73 'Foo': Foo(usize) -> Foo
70..82 'Foo(15 + 32)': Foo
74..76 '15': usize
74..81 '15 + 32': usize
79..81 '32': usize
88..90 '{}': ()
100..101 '_': Foo
105..107 '{}': ()
"#]],
);
}

View file

@ -1894,6 +1894,7 @@ fn effects_smoke_test() {
let x = unsafe { 92 };
let y = async { async { () }.await };
let z = try { () };
let w = const { 92 };
let t = 'a: { 92 };
}
@ -1905,7 +1906,7 @@ fn effects_smoke_test() {
}
"#,
expect![[r#"
16..136 '{ ...2 }; }': ()
16..162 '{ ...2 }; }': ()
26..27 'x': i32
30..43 'unsafe { 92 }': i32
37..43 '{ 92 }': i32
@ -1921,9 +1922,13 @@ fn effects_smoke_test() {
99..109 'try { () }': {unknown}
103..109 '{ () }': ()
105..107 '()': ()
119..120 't': i32
127..133 '{ 92 }': i32
129..131 '92': i32
119..120 'w': i32
123..135 'const { 92 }': i32
129..135 '{ 92 }': i32
131..133 '92': i32
145..146 't': i32
153..159 '{ 92 }': i32
155..157 '92': i32
"#]],
)
}