Skip to content

Add std::ops::Index support for infering #2592

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

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -254,6 +254,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