Skip to content

Fix ImplExpr::Builtin #1543

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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: 0 additions & 1 deletion cli/driver/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![feature(rustc_private)]
#![feature(box_patterns)]
#![feature(concat_idents)]
#![feature(trait_alias)]
#![allow(unused_imports)]
#![allow(unused_variables)]
Expand Down
18 changes: 10 additions & 8 deletions engine/lib/import_thir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ and c_item_unwrapped ~ident ~type_only (item : Thir.item) : item list =
in
(* TODO: things might be unnamed (e.g. constants) *)
match (item.kind : Thir.item_kind) with
| Const (_, _, generics, body) ->
| Const (_, generics, _, body) ->
mk
@@ Fn
{
Expand All @@ -1523,14 +1523,14 @@ and c_item_unwrapped ~ident ~type_only (item : Thir.item) : item list =
params = [];
safety = Safe;
}
| Static (_, _, true, _) ->
| Static (true, _, _, _) ->
unimplemented ~issue_id:1343 [ item.span ]
"Mutable static items are not supported."
| Static (_, _ty, false, body) ->
| Static (false, _, _ty, body) ->
let name = Concrete_ident.of_def_id ~value:true (assert_item_def_id ()) in
let generics = { params = []; constraints = [] } in
mk (Fn { name; generics; body = c_body body; params = []; safety = Safe })
| TyAlias (_, ty, generics) ->
| TyAlias (_, generics, ty) ->
mk
@@ TyAlias
{
Expand All @@ -1549,13 +1549,13 @@ and c_item_unwrapped ~ident ~type_only (item : Thir.item) : item list =
params = c_fn_params item.span params;
safety = c_header_safety safety;
}
| (Enum (_, _, generics, _) | Struct (_, _, generics)) when erased ->
| (Enum (_, generics, _, _) | Struct (_, generics, _)) when erased ->
let generics = c_generics generics in
let is_struct = match item.kind with Struct _ -> true | _ -> false in
let def_id = assert_item_def_id () in
let name = Concrete_ident.of_def_id ~value:false def_id in
mk @@ Type { name; generics; variants = []; is_struct }
| Enum (_, variants, generics, repr) ->
| Enum (_, generics, variants, repr) ->
let def_id = assert_item_def_id () in
let generics = c_generics generics in
let is_struct = false in
Expand Down Expand Up @@ -1613,7 +1613,7 @@ and c_item_unwrapped ~ident ~type_only (item : Thir.item) : item list =
mk_one (Type { name; generics; variants; is_struct }) :: discs
in
if is_primitive then cast_fun :: result else result
| Struct (_, v, generics) ->
| Struct (_, generics, v) ->
let generics = c_generics generics in
let def_id = assert_item_def_id () in
let is_struct = true in
Expand Down Expand Up @@ -1808,7 +1808,9 @@ and c_item_unwrapped ~ident ~type_only (item : Thir.item) : item list =
{
path = List.map ~f:(fun x -> fst x.ident) segments;
is_external =
List.exists ~f:(function Err -> true | _ -> false) res;
List.exists
~f:(function None | Some Err -> true | _ -> false)
res;
(* TODO: this should represent local/external? *)
rename;
}
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/exporter/src/constant_utils/uneval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, ConstantExpr> for ty::Const<'tcx>
let span = self.default_span(s.base().tcx);
match self.kind() {
ty::ConstKind::Param(p) => {
let ty = p.find_ty_from_env(s.param_env());
let ty = p.find_const_ty_from_env(s.param_env());
let kind = ConstantExprKind::ConstRef { id: p.sinto(s) };
kind.decorate(ty.sinto(s), span.sinto(s))
}
Expand Down Expand Up @@ -279,7 +279,7 @@ fn op_to_const<'tcx, S: UnderOwnerState<'tcx>>(
// Helper for struct-likes.
let read_fields = |of: rustc_const_eval::interpret::OpTy<'tcx>, field_count| {
(0..field_count).map(move |i| {
let field_op = ecx.project_field(&of, i)?;
let field_op = ecx.project_field(&of, rustc_abi::FieldIdx::from_usize(i))?;
op_to_const(s, span, &ecx, field_op)
})
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/exporter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![allow(rustdoc::private_intra_doc_links)]
#![cfg_attr(feature = "rustc", feature(concat_idents))]
#![cfg_attr(feature = "rustc", feature(if_let_guard))]
#![cfg_attr(feature = "rustc", feature(let_chains))]
#![cfg_attr(feature = "rustc", feature(macro_metavar_expr))]
#![cfg_attr(feature = "rustc", feature(rustc_private))]
#![cfg_attr(feature = "rustc", feature(sized_hierarchy))]
#![cfg_attr(feature = "rustc", feature(trait_alias))]
#![cfg_attr(feature = "rustc", feature(type_changing_struct_update))]

Expand Down
14 changes: 14 additions & 0 deletions frontend/exporter/src/sinto.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use crate::prelude::{derive_group, JsonSchema};

#[cfg(not(feature = "rustc"))]
pub trait SInto<S, To> {
fn sinto(&self, s: &S) -> To;
}

#[cfg(feature = "rustc")]
pub trait SInto<S, To>: std::marker::PointeeSized {
fn sinto(&self, s: &S) -> To;
}

#[macro_export]
macro_rules! sinto_todo {
($($mod:ident)::+, $type:ident$(<$($lts:lifetime),*$(,)?>)? as $renamed:ident) => {
Expand Down Expand Up @@ -59,6 +65,14 @@ impl<S, LL, RR, L: SInto<S, LL>, R: SInto<S, RR>> SInto<S, (LL, RR)> for (L, R)
}
}

impl<S, AA, BB, CC, A: SInto<S, AA>, B: SInto<S, BB>, C: SInto<S, CC>> SInto<S, (AA, BB, CC)>
for (A, B, C)
{
fn sinto(&self, s: &S) -> (AA, BB, CC) {
(self.0.sinto(s), self.1.sinto(s), self.2.sinto(s))
}
}

impl<S, D, T: SInto<S, D>> SInto<S, Option<D>> for Option<T> {
fn sinto(&self, s: &S) -> Option<D> {
self.as_ref().map(|x| x.sinto(s))
Expand Down
2 changes: 1 addition & 1 deletion frontend/exporter/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl ImplInfos {
.impl_trait_ref(did)
.map(|trait_ref| trait_ref.instantiate_identity())
.sinto(s),
clauses: predicates_defined_on(tcx, did).predicates.sinto(s),
clauses: predicates_defined_on(tcx, did).as_ref().sinto(s),
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions frontend/exporter/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ mod resolution;
mod utils;
#[cfg(feature = "rustc")]
pub use utils::{
erase_and_norm, implied_predicates, predicates_defined_on, required_predicates, self_predicate,
ToPolyTraitRef,
erase_and_norm, erase_free_regions, implied_predicates, normalize, predicates_defined_on,
required_predicates, self_predicate, Predicates, ToPolyTraitRef,
};

#[cfg(feature = "rustc")]
Expand Down Expand Up @@ -140,7 +140,7 @@ pub enum ImplExprAtom {
/// FnOnce`.
impl_exprs: Vec<ImplExpr>,
/// The values of the associated types for this trait.
types: Vec<(DefId, Ty)>,
types: Vec<(DefId, Ty, Vec<ImplExpr>)>,
},
/// An error happened while resolving traits.
Error(String),
Expand Down Expand Up @@ -290,7 +290,9 @@ pub fn translate_item_ref<'tcx, S: UnderOwnerState<'tcx>>(
let num_trait_generics = trait_ref.generic_args.len();
generic_args.drain(0..num_trait_generics);
let num_trait_trait_clauses = trait_ref.impl_exprs.len();
impl_exprs.drain(0..num_trait_trait_clauses);
// Associated items take a `Self` clause as first clause, we skip that one too. Note: that
// clause is the same as `tinfo`.
impl_exprs.drain(0..num_trait_trait_clauses + 1);
}

let content = ItemRefContents {
Expand Down Expand Up @@ -358,12 +360,11 @@ pub fn solve_item_implied_traits<'tcx, S: UnderOwnerState<'tcx>>(
fn solve_item_traits_inner<'tcx, S: UnderOwnerState<'tcx>>(
s: &S,
generics: ty::GenericArgsRef<'tcx>,
predicates: ty::GenericPredicates<'tcx>,
predicates: utils::Predicates<'tcx>,
) -> Vec<ImplExpr> {
let tcx = s.base().tcx;
let typing_env = s.typing_env();
predicates
.predicates
.iter()
.map(|(clause, _span)| *clause)
.filter_map(|clause| clause.as_trait_clause())
Expand All @@ -387,15 +388,14 @@ pub fn self_clause_for_item<'tcx, S: UnderOwnerState<'tcx>>(
def_id: RDefId,
generics: rustc_middle::ty::GenericArgsRef<'tcx>,
) -> Option<ImplExpr> {
use rustc_middle::ty::EarlyBinder;
let tcx = s.base().tcx;

let tr_def_id = tcx.trait_of_item(def_id)?;
// The "self" predicate in the context of the trait.
let self_pred = self_predicate(tcx, tr_def_id);
// Substitute to be in the context of the current item.
let generics = generics.truncate_to(tcx, tcx.generics_of(tr_def_id));
let self_pred = EarlyBinder::bind(self_pred).instantiate(tcx, generics);
let self_pred = ty::EarlyBinder::bind(self_pred).instantiate(tcx, generics);

// Resolve
Some(solve_trait(s, self_pred))
Expand Down
51 changes: 34 additions & 17 deletions frontend/exporter/src/traits/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use rustc_middle::traits::CodegenObligationError;
use rustc_middle::ty::{self, *};
use rustc_trait_selection::traits::ImplSource;

use crate::{self_predicate, traits::utils::erase_and_norm};

use super::utils::{implied_predicates, normalize_bound_val, required_predicates, ToPolyTraitRef};
use super::utils::{
self, erase_and_norm, implied_predicates, normalize_bound_val, required_predicates,
self_predicate, ToPolyTraitRef,
};

#[derive(Debug, Clone)]
pub enum PathChunk<'tcx> {
Expand Down Expand Up @@ -88,7 +89,7 @@ pub enum ImplExprAtom<'tcx> {
/// FnOnce`.
impl_exprs: Vec<ImplExpr<'tcx>>,
/// The values of the associated types for this trait.
types: Vec<(DefId, Ty<'tcx>)>,
types: Vec<(DefId, Ty<'tcx>, Vec<ImplExpr<'tcx>>)>,
},
/// An error happened while resolving traits.
Error(String),
Expand Down Expand Up @@ -153,6 +154,7 @@ fn initial_search_predicates<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: rustc_span::def_id::DefId,
add_drop: bool,
include_self_pred: bool,
predicates: &mut Vec<AnnotatedTraitPred<'tcx>>,
pred_id: &mut usize,
) {
Expand All @@ -164,11 +166,22 @@ fn initial_search_predicates<'tcx>(
use DefKind::*;
match tcx.def_kind(def_id) {
// These inherit some predicates from their parent.
AssocTy | AssocFn | AssocConst | Closure | Ctor(..) | Variant => {
dk @ (AssocTy | AssocFn | AssocConst | Closure | Ctor(..) | Variant) => {
let parent = tcx.parent(def_id);
acc_predicates(tcx, parent, add_drop, predicates, pred_id);
// Hack: we don't support GATs well so for now we let assoc types refer to the
// implicit trait `Self` clause. Other associated items get an explicit `Self:
// Trait` clause passed to them so they don't need that.
let include_self_pred = include_self_pred && matches!(dk, AssocTy);
acc_predicates(
tcx,
parent,
add_drop,
include_self_pred,
predicates,
pred_id,
);
}
Trait | TraitAlias => {
Trait | TraitAlias if include_self_pred => {
let self_pred = self_predicate(tcx, def_id).upcast(tcx);
predicates.push(AnnotatedTraitPred {
origin: BoundPredicateOrigin::SelfPred,
Expand All @@ -179,7 +192,6 @@ fn initial_search_predicates<'tcx>(
}
predicates.extend(
required_predicates(tcx, def_id, add_drop)
.predicates
.iter()
.map(|(clause, _span)| *clause)
.filter_map(|clause| {
Expand All @@ -192,7 +204,7 @@ fn initial_search_predicates<'tcx>(
}

let mut predicates = vec![];
acc_predicates(tcx, def_id, add_drop, &mut predicates, &mut 0);
acc_predicates(tcx, def_id, add_drop, true, &mut predicates, &mut 0);
predicates
}

Expand All @@ -204,7 +216,6 @@ fn parents_trait_predicates<'tcx>(
) -> Vec<PolyTraitPredicate<'tcx>> {
let self_trait_ref = pred.to_poly_trait_ref();
implied_predicates(tcx, pred.def_id(), add_drop)
.predicates
.iter()
.map(|(clause, _span)| *clause)
// Substitute with the `self` args so that the clause makes sense in the
Expand Down Expand Up @@ -342,8 +353,8 @@ impl<'tcx> PredicateSearcher<'tcx> {
};

// The bounds that hold on the associated type.
let item_bounds = implied_predicates(tcx, alias_ty.def_id, self.add_drop)
.predicates
let item_bounds = implied_predicates(tcx, alias_ty.def_id, self.add_drop);
let item_bounds = item_bounds
.iter()
.map(|(clause, _span)| *clause)
.filter_map(|pred| pred.as_trait_clause())
Expand Down Expand Up @@ -484,7 +495,14 @@ impl<'tcx> PredicateSearcher<'tcx> {
return None;
}
}
Some((assoc.def_id, ty))
let impl_exprs = self
.resolve_item_implied_predicates(
assoc.def_id,
erased_tref.skip_binder().args,
warn,
)
.ok()?;
Some((assoc.def_id, ty, impl_exprs))
})
.collect();
ImplExprAtom::Builtin {
Expand Down Expand Up @@ -642,13 +660,12 @@ impl<'tcx> PredicateSearcher<'tcx> {
pub fn resolve_predicates(
&mut self,
generics: GenericArgsRef<'tcx>,
predicates: GenericPredicates<'tcx>,
predicates: utils::Predicates<'tcx>,
// Call back into hax-related code to display a nice warning.
warn: &impl Fn(&str),
) -> Result<Vec<ImplExpr<'tcx>>, String> {
let tcx = self.tcx;
predicates
.predicates
.iter()
.map(|(clause, _span)| *clause)
.filter_map(|clause| clause.as_trait_clause())
Expand Down Expand Up @@ -677,7 +694,7 @@ pub fn shallow_resolve_trait_ref<'tcx>(
use rustc_middle::traits::CodegenObligationError;
use rustc_middle::ty::TypeVisitableExt;
use rustc_trait_selection::traits::{
Obligation, ObligationCause, ObligationCtxt, SelectionContext, Unimplemented,
Obligation, ObligationCause, ObligationCtxt, SelectionContext, SelectionError,
};
// Do the initial selection for the obligation. This yields the
// shallow result we are looking for -- that is, what specific impl.
Expand All @@ -693,7 +710,7 @@ pub fn shallow_resolve_trait_ref<'tcx>(
let selection = match selcx.poly_select(&obligation) {
Ok(Some(selection)) => selection,
Ok(None) => return Err(CodegenObligationError::Ambiguity),
Err(Unimplemented) => return Err(CodegenObligationError::Unimplemented),
Err(SelectionError::Unimplemented) => return Err(CodegenObligationError::Unimplemented),
Err(_) => return Err(CodegenObligationError::Ambiguity),
};

Expand Down
Loading
Loading