Skip to content

Commit 5df69d9

Browse files
bors[bot]crlf0710
andauthored
Merge #5993
5993: Update chalk to 0.27 and add support for extern opaque types. r=crlf0710 a=crlf0710 Co-authored-by: Charles Lew <[email protected]>
2 parents b14bf68 + 3fff5aa commit 5df69d9

File tree

15 files changed

+166
-44
lines changed

15 files changed

+166
-44
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_def/src/data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct TypeAliasData {
5454
pub name: Name,
5555
pub type_ref: Option<TypeRef>,
5656
pub visibility: RawVisibility,
57+
pub is_extern: bool,
5758
/// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
5859
pub bounds: Vec<TypeBound>,
5960
}
@@ -71,6 +72,7 @@ impl TypeAliasData {
7172
name: typ.name.clone(),
7273
type_ref: typ.type_ref.clone(),
7374
visibility: item_tree[typ.visibility].clone(),
75+
is_extern: typ.is_extern,
7476
bounds: typ.bounds.to_vec(),
7577
})
7678
}

crates/hir_def/src/item_tree.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ pub struct TypeAlias {
592592
pub bounds: Box<[TypeBound]>,
593593
pub generic_params: GenericParamsId,
594594
pub type_ref: Option<TypeRef>,
595+
pub is_extern: bool,
595596
pub ast_id: FileAstId<ast::TypeAlias>,
596597
}
597598

crates/hir_def/src/item_tree/lower.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ impl Ctx {
364364
generic_params,
365365
type_ref,
366366
ast_id,
367+
is_extern: false,
367368
};
368369
Some(id(self.data().type_aliases.alloc(res)))
369370
}
@@ -558,8 +559,9 @@ impl Ctx {
558559
statik.into()
559560
}
560561
ast::ExternItem::TypeAlias(ty) => {
561-
let id = self.lower_type_alias(&ty)?;
562-
id.into()
562+
let foreign_ty = self.lower_type_alias(&ty)?;
563+
self.data().type_aliases[foreign_ty.index].is_extern = true;
564+
foreign_ty.into()
563565
}
564566
ast::ExternItem::MacroCall(_) => return None,
565567
};

crates/hir_def/src/item_tree/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn smoke() {
236236
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("on_trait"))] }, input: None }]) }]
237237
Trait { name: Name(Text("Tr")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(0), auto: false, items: [TypeAlias(Idx::<TypeAlias>(0)), Const(Idx::<Const>(0)), Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<syntax::ast::generated::nodes::Trait>(2) }
238238
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_ty"))] }, input: None }]) }]
239-
> TypeAlias { name: Name(Text("AssocTy")), visibility: RawVisibilityId("pub(self)"), bounds: [Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Tr"))] }, generic_args: [Some(GenericArgs { args: [Type(Tuple([]))], has_self_type: false, bindings: [] })] })], generic_params: GenericParamsId(4294967295), type_ref: None, ast_id: FileAstId::<syntax::ast::generated::nodes::TypeAlias>(8) }
239+
> TypeAlias { name: Name(Text("AssocTy")), visibility: RawVisibilityId("pub(self)"), bounds: [Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Tr"))] }, generic_args: [Some(GenericArgs { args: [Type(Tuple([]))], has_self_type: false, bindings: [] })] })], generic_params: GenericParamsId(4294967295), type_ref: None, is_extern: false, ast_id: FileAstId::<syntax::ast::generated::nodes::TypeAlias>(8) }
240240
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_const"))] }, input: None }]) }]
241241
> Const { name: Some(Name(Text("CONST"))), visibility: RawVisibilityId("pub(self)"), type_ref: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("u8"))] }, generic_args: [None] }), ast_id: FileAstId::<syntax::ast::generated::nodes::Const>(9) }
242242
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_method"))] }, input: None }]) }]

crates/hir_ty/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ ena = "0.14.0"
1717
log = "0.4.8"
1818
rustc-hash = "1.1.0"
1919
scoped-tls = "1"
20-
chalk-solve = { version = "0.25.0" }
21-
chalk-ir = { version = "0.25.0" }
22-
chalk-recursive = { version = "0.25.0" }
20+
chalk-solve = { version = "0.27.0" }
21+
chalk-ir = { version = "0.27.0" }
22+
chalk-recursive = { version = "0.27.0" }
2323

2424
stdx = { path = "../stdx", version = "0.0.0" }
2525
hir_def = { path = "../hir_def", version = "0.0.0" }

crates/hir_ty/src/display.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ impl HirDisplay for ApplicationTy {
380380
write!(f, ">")?;
381381
}
382382
}
383+
TypeCtor::ForeignType(type_alias) => {
384+
let type_alias = f.db.type_alias_data(type_alias);
385+
write!(f, "{}", type_alias.name)?;
386+
if self.parameters.len() > 0 {
387+
write!(f, "<")?;
388+
f.write_joined(&*self.parameters.0, ", ")?;
389+
write!(f, ">")?;
390+
}
391+
}
383392
TypeCtor::OpaqueType(opaque_ty_id) => {
384393
match opaque_ty_id {
385394
OpaqueTyId::ReturnTypeImplTrait(func, idx) => {

crates/hir_ty/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ pub enum TypeCtor {
134134
/// representing the Future::Output type.
135135
OpaqueType(OpaqueTyId),
136136

137+
/// Represents a foreign type declared in external blocks.
138+
ForeignType(TypeAliasId),
139+
137140
/// The type of a specific closure.
138141
///
139142
/// The closure signature is stored in a `FnPtr` type in the first type
@@ -168,6 +171,10 @@ impl TypeCtor {
168171
let generic_params = generics(db.upcast(), type_alias.into());
169172
generic_params.len()
170173
}
174+
TypeCtor::ForeignType(type_alias) => {
175+
let generic_params = generics(db.upcast(), type_alias.into());
176+
generic_params.len()
177+
}
171178
TypeCtor::OpaqueType(opaque_ty_id) => {
172179
match opaque_ty_id {
173180
OpaqueTyId::ReturnTypeImplTrait(func, _) => {
@@ -204,6 +211,9 @@ impl TypeCtor {
204211
TypeCtor::AssociatedType(type_alias) => {
205212
Some(type_alias.lookup(db.upcast()).module(db.upcast()).krate)
206213
}
214+
TypeCtor::ForeignType(type_alias) => {
215+
Some(type_alias.lookup(db.upcast()).module(db.upcast()).krate)
216+
}
207217
TypeCtor::OpaqueType(opaque_ty_id) => match opaque_ty_id {
208218
OpaqueTyId::ReturnTypeImplTrait(func, _) => {
209219
Some(func.lookup(db.upcast()).module(db.upcast()).krate)
@@ -231,6 +241,7 @@ impl TypeCtor {
231241
TypeCtor::Adt(adt) => Some(adt.into()),
232242
TypeCtor::FnDef(callable) => Some(callable.into()),
233243
TypeCtor::AssociatedType(type_alias) => Some(type_alias.into()),
244+
TypeCtor::ForeignType(type_alias) => Some(type_alias.into()),
234245
TypeCtor::OpaqueType(_impl_trait_id) => None,
235246
}
236247
}

crates/hir_ty/src/lower.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,10 +1101,14 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
11011101
let resolver = t.resolver(db.upcast());
11021102
let ctx =
11031103
TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
1104-
let type_ref = &db.type_alias_data(t).type_ref;
11051104
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
1106-
let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error));
1107-
Binders::new(substs.len(), inner)
1105+
if db.type_alias_data(t).is_extern {
1106+
Binders::new(substs.len(), Ty::apply(TypeCtor::ForeignType(t), substs))
1107+
} else {
1108+
let type_ref = &db.type_alias_data(t).type_ref;
1109+
let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error));
1110+
Binders::new(substs.len(), inner)
1111+
}
11081112
}
11091113

11101114
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]

crates/hir_ty/src/method_resolution.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ impl Ty {
250250
TypeCtor::Adt(def_id) => {
251251
return Some(std::iter::once(def_id.module(db.upcast()).krate).collect())
252252
}
253+
TypeCtor::ForeignType(type_alias_id) => {
254+
return Some(
255+
std::iter::once(
256+
type_alias_id.lookup(db.upcast()).module(db.upcast()).krate,
257+
)
258+
.collect(),
259+
)
260+
}
253261
TypeCtor::Bool => lang_item_crate!("bool"),
254262
TypeCtor::Char => lang_item_crate!("char"),
255263
TypeCtor::Float(f) => match f.bitness {

0 commit comments

Comments
 (0)