Skip to content

Commit c3b8c2a

Browse files
committed
Auto merge of rust-lang#16996 - Veykril:lt-err, r=Veykril
internal: Lower outlive goals, respect them in display impls
2 parents 54faa03 + 707be6b commit c3b8c2a

File tree

23 files changed

+303
-122
lines changed

23 files changed

+303
-122
lines changed

crates/hir-def/src/find_path.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub fn find_path(
3030
find_path_inner(FindPathCtx { db, prefixed: None, prefer_no_std, prefer_prelude }, item, from)
3131
}
3232

33+
/// Find a path that can be used to refer to a certain item. This can depend on
34+
/// *from where* you're referring to the item, hence the `from` parameter.
3335
pub fn find_path_prefixed(
3436
db: &dyn DefDatabase,
3537
item: ItemInNs,
@@ -255,7 +257,7 @@ fn find_in_scope(
255257
item: ItemInNs,
256258
) -> Option<Name> {
257259
def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
258-
def_map[local_id].scope.name_of(item).map(|(name, _, _)| name.clone())
260+
def_map[local_id].scope.names_of(item, |name, _, _| Some(name.clone()))
259261
})
260262
}
261263

crates/hir-def/src/generics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use crate::{
2929
/// Data about a generic type parameter (to a function, struct, impl, ...).
3030
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
3131
pub struct TypeParamData {
32+
/// [`None`] only if the type ref is an [`TypeRef::ImplTrait`]. FIXME: Might be better to just
33+
/// make it always be a value, giving impl trait a special name.
3234
pub name: Option<Name>,
3335
pub default: Option<Interned<TypeRef>>,
3436
pub provenance: TypeParamProvenance,

crates/hir-def/src/item_scope.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,43 @@ impl ItemScope {
277277
ItemInNs::Types(def) => self.types.iter().find_map(|(name, &(other_def, vis, i))| {
278278
(other_def == def).then_some((name, vis, i.is_none()))
279279
}),
280-
281280
ItemInNs::Values(def) => self.values.iter().find_map(|(name, &(other_def, vis, i))| {
282281
(other_def == def).then_some((name, vis, i.is_none()))
283282
}),
284283
}
285284
}
286285

286+
/// XXX: this is O(N) rather than O(1), try to not introduce new usages.
287+
pub(crate) fn names_of<T>(
288+
&self,
289+
item: ItemInNs,
290+
mut cb: impl FnMut(&Name, Visibility, bool) -> Option<T>,
291+
) -> Option<T> {
292+
match item {
293+
ItemInNs::Macros(def) => self
294+
.macros
295+
.iter()
296+
.filter_map(|(name, &(other_def, vis, i))| {
297+
(other_def == def).then_some((name, vis, i.is_none()))
298+
})
299+
.find_map(|(a, b, c)| cb(a, b, c)),
300+
ItemInNs::Types(def) => self
301+
.types
302+
.iter()
303+
.filter_map(|(name, &(other_def, vis, i))| {
304+
(other_def == def).then_some((name, vis, i.is_none()))
305+
})
306+
.find_map(|(a, b, c)| cb(a, b, c)),
307+
ItemInNs::Values(def) => self
308+
.values
309+
.iter()
310+
.filter_map(|(name, &(other_def, vis, i))| {
311+
(other_def == def).then_some((name, vis, i.is_none()))
312+
})
313+
.find_map(|(a, b, c)| cb(a, b, c)),
314+
}
315+
}
316+
287317
pub(crate) fn traits(&self) -> impl Iterator<Item = TraitId> + '_ {
288318
self.types
289319
.values()

crates/hir-def/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ impl ModuleId {
422422
}
423423
}
424424

425+
pub fn crate_def_map(self, db: &dyn DefDatabase) -> Arc<DefMap> {
426+
db.crate_def_map(self.krate)
427+
}
428+
425429
pub fn krate(self) -> CrateId {
426430
self.krate
427431
}
@@ -438,6 +442,8 @@ impl ModuleId {
438442
})
439443
}
440444

445+
/// Returns the module containing `self`, either the parent `mod`, or the module (or block) containing
446+
/// the block, if `self` corresponds to a block expression.
441447
pub fn containing_module(self, db: &dyn DefDatabase) -> Option<ModuleId> {
442448
self.def_map(db).containing_module(self.local_id)
443449
}
@@ -929,6 +935,18 @@ impl GenericDefId {
929935
GenericDefId::EnumVariantId(_) => (FileId::BOGUS.into(), None),
930936
}
931937
}
938+
939+
pub fn assoc_trait_container(self, db: &dyn DefDatabase) -> Option<TraitId> {
940+
match match self {
941+
GenericDefId::FunctionId(f) => f.lookup(db).container,
942+
GenericDefId::TypeAliasId(t) => t.lookup(db).container,
943+
GenericDefId::ConstId(c) => c.lookup(db).container,
944+
_ => return None,
945+
} {
946+
ItemContainerId::TraitId(trait_) => Some(trait_),
947+
_ => None,
948+
}
949+
}
932950
}
933951

934952
impl From<AssocItemId> for GenericDefId {

crates/hir-ty/src/chalk_ext.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Various extensions traits for Chalk types.
22
3-
use chalk_ir::{cast::Cast, FloatTy, IntTy, Mutability, Scalar, TyVariableKind, UintTy};
3+
use chalk_ir::{
4+
cast::Cast, FloatTy, IntTy, Mutability, Scalar, TyVariableKind, TypeOutlives, UintTy,
5+
};
46
use hir_def::{
57
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
68
generics::TypeOrConstParamData,
@@ -312,14 +314,17 @@ impl TyExt for Ty {
312314
.generic_predicates(id.parent)
313315
.iter()
314316
.map(|pred| pred.clone().substitute(Interner, &substs))
315-
.filter(|wc| match &wc.skip_binders() {
317+
.filter(|wc| match wc.skip_binders() {
316318
WhereClause::Implemented(tr) => {
317319
&tr.self_type_parameter(Interner) == self
318320
}
319321
WhereClause::AliasEq(AliasEq {
320322
alias: AliasTy::Projection(proj),
321323
ty: _,
322324
}) => &proj.self_type_parameter(db) == self,
325+
WhereClause::TypeOutlives(TypeOutlives { ty, lifetime: _ }) => {
326+
ty == self
327+
}
323328
_ => false,
324329
})
325330
.collect::<Vec<_>>();

crates/hir-ty/src/display.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99

1010
use base_db::CrateId;
1111
use chalk_ir::{BoundVar, Safety, TyKind};
12+
use either::Either;
1213
use hir_def::{
1314
data::adt::VariantData,
1415
db::DefDatabase,
@@ -1072,6 +1073,7 @@ impl HirDisplay for Ty {
10721073
write_bounds_like_dyn_trait_with_prefix(
10731074
f,
10741075
"impl",
1076+
Either::Left(self),
10751077
bounds.skip_binders(),
10761078
SizedByDefault::Sized { anchor: krate },
10771079
)?;
@@ -1087,6 +1089,7 @@ impl HirDisplay for Ty {
10871089
write_bounds_like_dyn_trait_with_prefix(
10881090
f,
10891091
"impl",
1092+
Either::Left(self),
10901093
bounds.skip_binders(),
10911094
SizedByDefault::Sized { anchor: krate },
10921095
)?;
@@ -1189,21 +1192,24 @@ impl HirDisplay for Ty {
11891192
.generic_predicates(id.parent)
11901193
.iter()
11911194
.map(|pred| pred.clone().substitute(Interner, &substs))
1192-
.filter(|wc| match &wc.skip_binders() {
1195+
.filter(|wc| match wc.skip_binders() {
11931196
WhereClause::Implemented(tr) => {
1194-
&tr.self_type_parameter(Interner) == self
1197+
tr.self_type_parameter(Interner) == *self
11951198
}
11961199
WhereClause::AliasEq(AliasEq {
11971200
alias: AliasTy::Projection(proj),
11981201
ty: _,
1199-
}) => &proj.self_type_parameter(db) == self,
1200-
_ => false,
1202+
}) => proj.self_type_parameter(db) == *self,
1203+
WhereClause::AliasEq(_) => false,
1204+
WhereClause::TypeOutlives(to) => to.ty == *self,
1205+
WhereClause::LifetimeOutlives(_) => false,
12011206
})
12021207
.collect::<Vec<_>>();
12031208
let krate = id.parent.module(db.upcast()).krate();
12041209
write_bounds_like_dyn_trait_with_prefix(
12051210
f,
12061211
"impl",
1212+
Either::Left(self),
12071213
&bounds,
12081214
SizedByDefault::Sized { anchor: krate },
12091215
)?;
@@ -1229,6 +1235,7 @@ impl HirDisplay for Ty {
12291235
write_bounds_like_dyn_trait_with_prefix(
12301236
f,
12311237
"dyn",
1238+
Either::Left(self),
12321239
&bounds,
12331240
SizedByDefault::NotSized,
12341241
)?;
@@ -1252,6 +1259,7 @@ impl HirDisplay for Ty {
12521259
write_bounds_like_dyn_trait_with_prefix(
12531260
f,
12541261
"impl",
1262+
Either::Left(self),
12551263
bounds.skip_binders(),
12561264
SizedByDefault::Sized { anchor: krate },
12571265
)?;
@@ -1266,6 +1274,7 @@ impl HirDisplay for Ty {
12661274
write_bounds_like_dyn_trait_with_prefix(
12671275
f,
12681276
"impl",
1277+
Either::Left(self),
12691278
bounds.skip_binders(),
12701279
SizedByDefault::Sized { anchor: krate },
12711280
)?;
@@ -1468,6 +1477,7 @@ impl SizedByDefault {
14681477
pub fn write_bounds_like_dyn_trait_with_prefix(
14691478
f: &mut HirFormatter<'_>,
14701479
prefix: &str,
1480+
this: Either<&Ty, &Lifetime>,
14711481
predicates: &[QuantifiedWhereClause],
14721482
default_sized: SizedByDefault,
14731483
) -> Result<(), HirDisplayError> {
@@ -1476,14 +1486,15 @@ pub fn write_bounds_like_dyn_trait_with_prefix(
14761486
|| predicates.is_empty() && matches!(default_sized, SizedByDefault::Sized { .. })
14771487
{
14781488
write!(f, " ")?;
1479-
write_bounds_like_dyn_trait(f, predicates, default_sized)
1489+
write_bounds_like_dyn_trait(f, this, predicates, default_sized)
14801490
} else {
14811491
Ok(())
14821492
}
14831493
}
14841494

14851495
fn write_bounds_like_dyn_trait(
14861496
f: &mut HirFormatter<'_>,
1497+
this: Either<&Ty, &Lifetime>,
14871498
predicates: &[QuantifiedWhereClause],
14881499
default_sized: SizedByDefault,
14891500
) -> Result<(), HirDisplayError> {
@@ -1541,6 +1552,28 @@ fn write_bounds_like_dyn_trait(
15411552
}
15421553
}
15431554
}
1555+
WhereClause::TypeOutlives(to) if Either::Left(&to.ty) == this => {
1556+
if !is_fn_trait && angle_open {
1557+
write!(f, ">")?;
1558+
angle_open = false;
1559+
}
1560+
if !first {
1561+
write!(f, " + ")?;
1562+
}
1563+
to.lifetime.hir_fmt(f)?;
1564+
}
1565+
WhereClause::TypeOutlives(_) => {}
1566+
WhereClause::LifetimeOutlives(lo) if Either::Right(&lo.a) == this => {
1567+
if !is_fn_trait && angle_open {
1568+
write!(f, ">")?;
1569+
angle_open = false;
1570+
}
1571+
if !first {
1572+
write!(f, " + ")?;
1573+
}
1574+
lo.b.hir_fmt(f)?;
1575+
}
1576+
WhereClause::LifetimeOutlives(_) => {}
15441577
WhereClause::AliasEq(alias_eq) if is_fn_trait => {
15451578
is_fn_trait = false;
15461579
if !alias_eq.ty.is_unit() {
@@ -1577,10 +1610,6 @@ fn write_bounds_like_dyn_trait(
15771610
}
15781611
ty.hir_fmt(f)?;
15791612
}
1580-
1581-
// FIXME implement these
1582-
WhereClause::LifetimeOutlives(_) => {}
1583-
WhereClause::TypeOutlives(_) => {}
15841613
}
15851614
first = false;
15861615
}

crates/hir-ty/src/infer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ use triomphe::Arc;
5555

5656
use crate::{
5757
db::HirDatabase,
58-
fold_tys,
58+
error_lifetime, fold_tys,
5959
infer::{coerce::CoerceMany, unify::InferenceTable},
6060
lower::ImplTraitLoweringMode,
61-
static_lifetime, to_assoc_type_id,
61+
to_assoc_type_id,
6262
traits::FnTrait,
6363
utils::{InTypeConstIdMetadata, UnevaluatedConstEvaluatorFolder},
6464
AliasEq, AliasTy, Binders, ClosureId, Const, DomainGoal, GenericArg, Goal, ImplTraitId,
@@ -326,7 +326,7 @@ pub struct Adjustment {
326326

327327
impl Adjustment {
328328
pub fn borrow(m: Mutability, ty: Ty) -> Self {
329-
let ty = TyKind::Ref(m, static_lifetime(), ty).intern(Interner);
329+
let ty = TyKind::Ref(m, error_lifetime(), ty).intern(Interner);
330330
Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(m)), target: ty }
331331
}
332332
}

crates/hir-ty/src/infer/closure.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use stdx::never;
2222

2323
use crate::{
2424
db::{HirDatabase, InternedClosure},
25-
from_chalk_trait_id, from_placeholder_idx, make_binders,
25+
error_lifetime, from_chalk_trait_id, from_placeholder_idx, make_binders,
2626
mir::{BorrowKind, MirSpan, MutBorrowKind, ProjectionElem},
27-
static_lifetime, to_chalk_trait_id,
27+
to_chalk_trait_id,
2828
traits::FnTrait,
2929
utils::{self, elaborate_clause_supertraits, generics, Generics},
3030
Adjust, Adjustment, AliasEq, AliasTy, Binders, BindingMode, ChalkTraitId, ClosureId, DynTy,
@@ -324,7 +324,7 @@ impl CapturedItemWithoutTy {
324324
BorrowKind::Mut { .. } => Mutability::Mut,
325325
_ => Mutability::Not,
326326
};
327-
TyKind::Ref(m, static_lifetime(), ty).intern(Interner)
327+
TyKind::Ref(m, error_lifetime(), ty).intern(Interner)
328328
}
329329
};
330330
return CapturedItem {

crates/hir-ty/src/infer/coerce.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use triomphe::Arc;
1818
use crate::{
1919
autoderef::{Autoderef, AutoderefKind},
2020
db::HirDatabase,
21+
error_lifetime,
2122
infer::{
2223
Adjust, Adjustment, AutoBorrow, InferOk, InferenceContext, OverloadedDeref, PointerCast,
2324
TypeError, TypeMismatch,
2425
},
25-
static_lifetime,
2626
utils::ClosureSubst,
2727
Canonical, DomainGoal, FnAbi, FnPointer, FnSig, Guidance, InEnvironment, Interner, Solution,
2828
Substitution, TraitEnvironment, Ty, TyBuilder, TyExt,
@@ -427,7 +427,7 @@ impl InferenceTable<'_> {
427427
// compare those. Note that this means we use the target
428428
// mutability [1], since it may be that we are coercing
429429
// from `&mut T` to `&U`.
430-
let lt = static_lifetime(); // FIXME: handle lifetimes correctly, see rustc
430+
let lt = error_lifetime(); // FIXME: handle lifetimes correctly, see rustc
431431
let derefd_from_ty = TyKind::Ref(to_mt, lt, referent_ty).intern(Interner);
432432
match autoderef.table.try_unify(&derefd_from_ty, to_ty) {
433433
Ok(result) => {
@@ -621,7 +621,7 @@ impl InferenceTable<'_> {
621621
(TyKind::Ref(from_mt, _, from_inner), &TyKind::Ref(to_mt, _, _)) => {
622622
coerce_mutabilities(*from_mt, to_mt)?;
623623

624-
let lt = static_lifetime();
624+
let lt = error_lifetime();
625625
Some((
626626
Adjustment { kind: Adjust::Deref(None), target: from_inner.clone() },
627627
Adjustment {

crates/hir-ty/src/infer/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::{
2323
autoderef::{builtin_deref, deref_by_trait, Autoderef},
2424
consteval,
2525
db::{InternedClosure, InternedCoroutine},
26+
error_lifetime,
2627
infer::{
2728
coerce::{CoerceMany, CoercionCause},
2829
find_continuable,
@@ -630,7 +631,7 @@ impl InferenceContext<'_> {
630631
let inner_ty = self.infer_expr_inner(*expr, &expectation);
631632
match rawness {
632633
Rawness::RawPtr => TyKind::Raw(mutability, inner_ty),
633-
Rawness::Ref => TyKind::Ref(mutability, static_lifetime(), inner_ty),
634+
Rawness::Ref => TyKind::Ref(mutability, error_lifetime(), inner_ty),
634635
}
635636
.intern(Interner)
636637
}

0 commit comments

Comments
 (0)