Skip to content
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

Remove workaround in trait resolution for associated items #990

Merged
merged 1 commit into from
Oct 14, 2024
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
2 changes: 1 addition & 1 deletion frontend/exporter/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub mod rustc {
}

// Lifetimes are irrelevant when resolving instances.
pub(super) fn erase_and_norm<'tcx, T>(
pub(crate) fn erase_and_norm<'tcx, T>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
x: T,
Expand Down
34 changes: 18 additions & 16 deletions frontend/exporter/src/types/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1775,31 +1775,33 @@ pub enum AliasKind {
#[cfg(feature = "rustc")]
impl Alias {
#[tracing::instrument(level = "trace", skip(s))]
fn from<'tcx, S: BaseState<'tcx> + HasOwnerId>(
fn from<'tcx, S: UnderOwnerState<'tcx>>(
s: &S,
alias_kind: &rustc_type_ir::AliasTyKind,
alias_ty: &rustc_middle::ty::AliasTy<'tcx>,
) -> Self {
use rustc_type_ir::AliasTyKind as RustAliasKind;
let kind = match alias_kind {
RustAliasKind::Projection => {
use rustc_middle::ty::{Binder, TypeVisitableExt};
let tcx = s.base().tcx;
let trait_ref = alias_ty.trait_ref(tcx);
// We don't have a clear handling of binders here; this is causing a number of
// problems in Charon. In the meantime we return something well-formed when we
// can't trait-solve. See also https://github.com/hacspec/hax/issues/495.
if trait_ref.has_escaping_bound_vars() {
warning!(
s,
"Hax frontend found a projected type with escaping bound vars. Please report https://github.com/hacspec/hax/issues/495"
);
AliasKind::Opaque
} else {
AliasKind::Projection {
assoc_item: tcx.associated_item(alias_ty.def_id).sinto(s),
impl_expr: solve_trait(s, Binder::dummy(trait_ref)),
}
// In a case like:
// ```
// impl<T, U> Trait for Result<T, U>
// where
// for<'a> &'a Result<T, U>: IntoIterator,
// for<'a> <&'a Result<T, U> as IntoIterator>::Item: Copy,
// {}
// ```
// the `&'a Result<T, U> as IntoIterator` trait ref has escaping bound variables
// yet we dont have a binder around (could even be several). Binding this correctly
// is therefore difficult. Since our trait resolution ignores lifetimes anyway, we
// just erase them. See also https://github.com/hacspec/hax/issues/747.
let trait_ref =
traits::rustc::search_clause::erase_and_norm(tcx, s.param_env(), trait_ref);
AliasKind::Projection {
assoc_item: tcx.associated_item(alias_ty.def_id).sinto(s),
impl_expr: solve_trait(s, ty::Binder::dummy(trait_ref)),
}
}
RustAliasKind::Inherent => AliasKind::Inherent,
Expand Down
Loading