Skip to content

Commit

Permalink
Merge #2592
Browse files Browse the repository at this point in the history
2592: Add std::ops::Index support for infering r=edwin0cheng a=edwin0cheng

see also #2534

Seem like this can't fix #2534 for this case:

```rust
fn foo3(bar: [usize; 2]) {
    let baz = bar[1];   // <--- baz is still unknown ?
    println!("{}", baz);
}
```

Co-authored-by: Edwin Cheng <[email protected]>
  • Loading branch information
bors[bot] and edwin0cheng authored Dec 20, 2019
2 parents 81a1b14 + 76d688a commit d590f6c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/ra_hir_def/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ macro_rules! __known_path {
(std::ops::Try) => {};
(std::ops::Neg) => {};
(std::ops::Not) => {};
(std::ops::Index) => {};
($path:path) => {
compile_error!("Please register your known path in the path module")
};
Expand Down
1 change: 1 addition & 0 deletions crates/ra_hir_expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub mod known {
Range,
Neg,
Not,
Index,
// Builtin macros
file,
column,
Expand Down
20 changes: 19 additions & 1 deletion crates/ra_hir_ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}

fn resolve_associated_type(&mut self, inner_ty: Ty, assoc_ty: Option<TypeAliasId>) -> Ty {
self.resolve_associated_type_with_params(inner_ty, assoc_ty, &[])
}

fn resolve_associated_type_with_params(
&mut self,
inner_ty: Ty,
assoc_ty: Option<TypeAliasId>,
params: &[Ty],
) -> Ty {
match assoc_ty {
Some(res_assoc_ty) => {
let ty = self.table.new_type_var();
let builder = Substs::build_for_def(self.db, res_assoc_ty)
.push(inner_ty)
.fill(params.iter().cloned());
let projection = ProjectionPredicate {
ty: ty.clone(),
projection_ty: ProjectionTy {
associated_ty: res_assoc_ty,
parameters: Substs::single(inner_ty),
parameters: builder.build(),
},
};
self.obligations.push(Obligation::Projection(projection));
Expand Down Expand Up @@ -517,6 +529,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}

fn resolve_ops_index_output(&self) -> Option<TypeAliasId> {
let path = path![std::ops::Index];
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Output])
}
}

/// The kinds of placeholders we need during type inference. There's separate
Expand Down
12 changes: 8 additions & 4 deletions crates/ra_hir_ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}
}
Expr::Index { base, index } => {
let _base_ty = self.infer_expr_inner(*base, &Expectation::none());
let _index_ty = self.infer_expr(*index, &Expectation::none());
// FIXME: use `std::ops::Index::Output` to figure out the real return type
Ty::Unknown
let base_ty = self.infer_expr_inner(*base, &Expectation::none());
let index_ty = self.infer_expr(*index, &Expectation::none());

self.resolve_associated_type_with_params(
base_ty,
self.resolve_ops_index_output(),
&[index_ty],
)
}
Expr::Tuple { exprs } => {
let mut tys = match &expected.ty {
Expand Down
32 changes: 32 additions & 0 deletions crates/ra_hir_ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,38 @@ fn indexing_arrays() {
)
}

#[test]
fn infer_ops_index() {
let (db, pos) = TestDB::with_position(
r#"
//- /main.rs crate:main deps:std
struct Bar;
struct Foo;
impl std::ops::Index<u32> for Bar {
type Output = Foo;
}
fn test() {
let a = Bar;
let b = a[1];
b<|>;
}
//- /std.rs crate:std
#[prelude_import] use ops::*;
mod ops {
pub trait Index<Idx> {
type Output;
}
}
"#,
);
assert_eq!("Foo", type_at_pos(&db, pos));
}

#[test]
fn deref_trait() {
let t = type_at(
Expand Down

0 comments on commit d590f6c

Please sign in to comment.