Skip to content

Commit aa29866

Browse files
authored
Merge pull request #1613 from Nadrieril/blah
Some more assorted fixes
2 parents bfb0196 + 5b171a8 commit aa29866

93 files changed

Lines changed: 1462 additions & 948 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎charon/rustc_trait_elaboration/src/elaboration.rs‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,25 @@ impl<'tcx, Id: ItemId> PredicateSearcher<'tcx, Id> {
488488
impl_def_id,
489489
args: generics,
490490
..
491-
})) => TraitProofKind::Concrete(self.resolve_item_reference(
492-
state,
493-
Id::from_rust_def_id(state, impl_def_id),
494-
generics,
495-
AssocItemResolution::ImplItem,
496-
)),
491+
})) => {
492+
// Selection can return an impl for `fn(&'_ ())` when we needed one for `for<'a>
493+
// fn(&'a ())`. So we check that the returned impl does prove the predicate we
494+
// want, modulo free regions.
495+
let proved_predicate = erase_and_norm(
496+
tcx,
497+
self.typing_env,
498+
tcx.impl_trait_ref(impl_def_id).instantiate(tcx, generics),
499+
);
500+
if proved_predicate != erased_tref.skip_binder() {
501+
return error(format!("Could not find a proof for `{erased_tref}`"));
502+
}
503+
TraitProofKind::Concrete(self.resolve_item_reference(
504+
state,
505+
Id::from_rust_def_id(state, impl_def_id),
506+
generics,
507+
AssocItemResolution::ImplItem,
508+
))
509+
}
497510
// Rustc can report ambiguity for a built-in trait's own `Self` clause. In that case
498511
// the clause can be found locally, so we try that.
499512
Err(CodegenObligationError::Ambiguity) | Ok(ImplSource::Param(_)) => match self
@@ -511,13 +524,10 @@ impl<'tcx, Id: ItemId> PredicateSearcher<'tcx, Id> {
511524
}
512525
Ok(ImplSource::Builtin(_, _)) => {
513526
// Resolve the predicates implied by the trait.
514-
// If we wanted to not skip this binder, we'd have to instantiate the bound
515-
// regions, solve, then wrap the result in a binder. And track higher-kinded
516-
// clauses better all over.
517527
let trait_proofs = self.resolve_item_implied_predicates(
518528
state,
519529
Id::from_rust_def_id(state, trait_def_id),
520-
erased_tref.skip_binder().args,
530+
erase_binder(tcx, tref).args,
521531
);
522532
let types = tcx
523533
.associated_items(trait_def_id)

‎charon/rustc_trait_elaboration/src/utils.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ where
6868
.unwrap_or(fallback)
6969
}
7070

71+
/// Just like `tcx.instantiate_bound_regions_with_erased`, but works under binders.
72+
pub fn erase_binder<'tcx, T>(tcx: TyCtxt<'tcx>, value: Binder<'tcx, T>) -> T
73+
where
74+
T: TypeFoldable<TyCtxt<'tcx>>,
75+
{
76+
fold_regions(tcx, value.skip_binder(), |r, depth| match r.kind() {
77+
ReBound(BoundVarIndexKind::Bound(db), _) if db == depth => tcx.lifetimes.re_erased,
78+
ReBound(BoundVarIndexKind::Bound(db), br) => {
79+
// `fold_regions` skips regions bound inside the value.
80+
Region::new_bound(tcx, db.shifted_out(1), br)
81+
}
82+
_ => r,
83+
})
84+
}
85+
7186
/// Erase free regions from the given value. Largely copied from `tcx.erase_and_anonymize_regions`, but also
7287
/// erases bound regions that are bound outside `value`, so we can call this function inside a
7388
/// `Binder`.

‎charon/src/ast/items/fun_decl.rs‎

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -174,88 +174,6 @@ impl FunDecl {
174174
}
175175
}
176176

177-
impl RegionBinder<FunSig> {
178-
/// Infer how the bound regions of `self` appear in `target`. The returned binder binds the
179-
/// same regions as `target`; other lifetimes may differ.
180-
pub fn infer_region_mapping(&self, target: &Self) -> Option<RegionBinder<RegionArgs>> {
181-
struct RegionMapping {
182-
regions: IndexVec<RegionId, Option<Region>>,
183-
depth: DeBruijnId,
184-
}
185-
186-
impl Visitor for RegionMapping {
187-
type Break = ();
188-
}
189-
190-
impl ZipAst for RegionMapping {
191-
fn visit_region_param(
192-
&mut self,
193-
_source: &RegionParam,
194-
_target: &RegionParam,
195-
) -> ControlFlow<()> {
196-
// Binder positions identify regions; names and metadata need not match.
197-
ControlFlow::Continue(())
198-
}
199-
200-
fn visit_region(&mut self, source: &Region, target: &Region) -> ControlFlow<()> {
201-
if let (Region::Lub(source), Region::Lub(target)) = (source, target) {
202-
return self.visit(source, target);
203-
}
204-
// We're only concerned with mapping bound variables.
205-
if let Region::Var(var @ DeBruijnVar::Bound(depth, _)) = source
206-
&& *depth <= self.depth
207-
{
208-
if let Some(id) = var.bound_at_depth(self.depth) {
209-
let Some(target) = target.clone().move_from_under_binders(self.depth)
210-
else {
211-
return ControlFlow::Break(());
212-
};
213-
let mapped = &mut self.regions[id];
214-
if let Some(previous) = mapped
215-
&& *previous != target
216-
{
217-
return ControlFlow::Break(());
218-
}
219-
*mapped = Some(target);
220-
ControlFlow::Continue(())
221-
} else if source != target {
222-
ControlFlow::Break(())
223-
} else {
224-
ControlFlow::Continue(())
225-
}
226-
} else {
227-
ControlFlow::Continue(())
228-
}
229-
}
230-
231-
fn visit_region_binder<T: AstVisitable>(
232-
&mut self,
233-
source: &RegionBinder<T>,
234-
target: &RegionBinder<T>,
235-
) -> ControlFlow<()> {
236-
self.depth = self.depth.incr();
237-
let result = self.visit_inner(source, target);
238-
self.depth = self.depth.decr();
239-
result
240-
}
241-
}
242-
243-
let mut mapping = RegionMapping {
244-
regions: self.regions.map_ref_indexed(|_, _| None),
245-
depth: DeBruijnId::zero(),
246-
};
247-
if ZipAst::visit(&mut mapping, &self.skip_binder, &target.skip_binder).is_break() {
248-
return None;
249-
}
250-
let regions = RegionArgs::new(mapping.regions.into_iter().collect::<Option<_>>()?);
251-
Some(RegionBinder {
252-
regions: target.regions.clone(),
253-
outlives: target.outlives.clone(),
254-
skip_binder: regions,
255-
})
256-
}
257-
}
258-
259177
impl Abi {
260178
pub fn rust() -> Self {
261179
Self::Rust

‎charon/src/ast/type_level.rs‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,89 @@ impl<T> RegionBinder<T> {
735735
let args = RegionArgs::erased(self.regions.len());
736736
self.apply(args)
737737
}
738+
739+
/// Infer how the bound regions of `self` appear in `target`. The returned binder binds the
740+
/// same regions as `target`; other lifetimes may differ.
741+
pub fn infer_region_mapping(&self, target: &Self) -> Option<RegionBinder<RegionArgs>>
742+
where
743+
T: TyVisitable,
744+
{
745+
struct RegionMapping {
746+
regions: IndexVec<RegionId, Option<Region>>,
747+
depth: DeBruijnId,
748+
}
749+
750+
impl Visitor for RegionMapping {
751+
type Break = ();
752+
}
753+
754+
impl ZipAst for RegionMapping {
755+
fn visit_region_param(
756+
&mut self,
757+
_source: &RegionParam,
758+
_target: &RegionParam,
759+
) -> ControlFlow<()> {
760+
// Binder positions identify regions; names and metadata need not match.
761+
ControlFlow::Continue(())
762+
}
763+
764+
fn visit_region(&mut self, source: &Region, target: &Region) -> ControlFlow<()> {
765+
if let (Region::Lub(source), Region::Lub(target)) = (source, target) {
766+
return self.visit(source, target);
767+
}
768+
// We're only concerned with mapping bound variables.
769+
if let Region::Var(var @ DeBruijnVar::Bound(depth, _)) = source
770+
&& *depth <= self.depth
771+
{
772+
if let Some(id) = var.bound_at_depth(self.depth) {
773+
let Some(target) = target.clone().move_from_under_binders(self.depth)
774+
else {
775+
return ControlFlow::Break(());
776+
};
777+
let mapped = &mut self.regions[id];
778+
if let Some(previous) = mapped
779+
&& *previous != target
780+
{
781+
return ControlFlow::Break(());
782+
}
783+
*mapped = Some(target);
784+
ControlFlow::Continue(())
785+
} else if source != target {
786+
ControlFlow::Break(())
787+
} else {
788+
ControlFlow::Continue(())
789+
}
790+
} else {
791+
ControlFlow::Continue(())
792+
}
793+
}
794+
795+
fn visit_region_binder<T: AstVisitable>(
796+
&mut self,
797+
source: &RegionBinder<T>,
798+
target: &RegionBinder<T>,
799+
) -> ControlFlow<()> {
800+
self.depth = self.depth.incr();
801+
let result = self.visit_inner(source, target);
802+
self.depth = self.depth.decr();
803+
result
804+
}
805+
}
806+
807+
let mut mapping = RegionMapping {
808+
regions: self.regions.map_ref_indexed(|_, _| None),
809+
depth: DeBruijnId::zero(),
810+
};
811+
if ZipAst::visit(&mut mapping, &self.skip_binder, &target.skip_binder).is_break() {
812+
return None;
813+
}
814+
let regions = RegionArgs::new(mapping.regions.into_iter().collect::<Option<_>>()?);
815+
Some(RegionBinder {
816+
regions: target.regions.clone(),
817+
outlives: target.outlives.clone(),
818+
skip_binder: regions,
819+
})
820+
}
738821
}
739822

740823
impl<Id: TypeLevelVar> std::ops::Index<Id> for GenericArgs {

‎charon/src/bin/charon-driver/translate/user_type_annotations.rs‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,16 @@ impl UserTypeAnnotations {
103103
Ok(recovered)
104104
}
105105

106-
/// Get the annotated type of this local, if there was such an annotation, and any additional
107-
/// constraints from other annotations on the same local.
106+
/// Get the type of this local and the constraints from its type annotations.
108107
pub(crate) fn translate_local_ty<'tcx>(
109108
&self,
110109
ctx: &mut ItemTransCtx<'tcx, '_>,
111110
span: Span,
112111
local_id: LocalId,
113112
decl: &mir::LocalDecl<'tcx>,
114113
) -> Result<(Ty, Vec<PlaceTypeConstraint>), Error> {
114+
let ty = ctx.translate_rustc_ty(span, &decl.ty)?;
115+
let mut constraints = Vec::new();
115116
if let Some(user_tys) = &decl.user_ty
116117
&& user_tys.projections().len() != 0
117118
{
@@ -120,14 +121,13 @@ impl UserTypeAnnotations {
120121
} else {
121122
// In `let ref x: T`, the annotation describes the pointer target. We grab the
122123
// appropriate region by using the normal type of the local.
123-
let inferred_ty = ctx.translate_rustc_ty(span, &decl.ty)?;
124-
let TyKind::Ref(region, _, kind) = inferred_ty.kind() else {
124+
let TyKind::Ref(region, _, kind) = ty.kind() else {
125125
raise_error!(ctx, span, "ref binding has non-reference type");
126126
};
127127
Some((region.clone(), *kind))
128128
};
129129

130-
let mut annotated_tys = user_tys
130+
let annotated_tys = user_tys
131131
.projections()
132132
.map(|pj| self.translate_projected_annotation(pj))
133133
.map(|ty| {
@@ -137,19 +137,15 @@ impl UserTypeAnnotations {
137137
ty
138138
}
139139
});
140-
let ty = annotated_tys.next().unwrap();
141-
let mut constraints = Vec::new();
142140
for annotated_ty in annotated_tys {
143141
constraints.push(PlaceTypeConstraint {
144142
place: Place::new(local_id, ty.clone()),
145143
ty: annotated_ty,
146-
variance: Variance::Invariant,
144+
variance: Variance::Covariant,
147145
});
148146
}
149-
Ok((ty, constraints))
150-
} else {
151-
Ok((ctx.translate_rustc_ty(span, &decl.ty)?, Vec::new()))
152147
}
148+
Ok((ty, constraints))
153149
}
154150

155151
/// Look up the (unprojected) type of an annotation.

0 commit comments

Comments
 (0)