Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
6cee56a
Update rustc
Nadrieril Jul 1, 2025
1ca3abe
Don't overuse the `tcx` arena
Nadrieril Jul 1, 2025
7dac848
Add an explicit `Self: Trait` clause to trait assoc items
Nadrieril Jul 2, 2025
358c68c
Merge branch 'explicit-self-clause' into dev
Nadrieril Jul 2, 2025
6471ff2
Don't add `Drop` clauses to some more marker traits
Nadrieril Jul 3, 2025
ae0f212
Normalize projections before translating them
Nadrieril Jul 3, 2025
8c92b50
Resolve assoc ty predicates in builtin `ImplExpr`s
Nadrieril Jul 3, 2025
7382371
Merge branch 'fix-builtin-impl-exprs' into dev
Nadrieril Jul 3, 2025
407c121
Don't use `SInto` derive for `FullDefKind`
Nadrieril Jun 20, 2025
6ed2039
Don't add drop bounds for closure fake type params
Nadrieril Jul 7, 2025
904ed63
Make `Drop` bounds on traits be implied instead of required
Nadrieril Jul 7, 2025
55ef02f
Don't add `Drop` bounds to the `Tuple` marker trait
Nadrieril Jul 7, 2025
9981e4e
Provide resolved `ImplExpr`s for virtual trait impls
Nadrieril Jul 7, 2025
0d9d42a
Merge branch 'fix-drops' into dev
Nadrieril Jul 7, 2025
b955547
Update rustc again
Nadrieril Jul 9, 2025
6e6d25d
Merge branch 'update-rustc' into dev
Nadrieril Jul 9, 2025
c73c480
Add variance information to generic parameters
Nadrieril Jul 9, 2025
855ed3e
Merge branch 'variance' into dev
Nadrieril Jul 9, 2025
e2d69df
Cleanup the creation of `State` structs
Nadrieril Jul 8, 2025
8c3e3a7
Remove `HasXXXSetter` traits
Nadrieril Jul 8, 2025
78946d1
Reorder `State` fields
Nadrieril Jul 10, 2025
29ba2d6
Track the type of surrounding expression in THIR translation
Nadrieril Jul 10, 2025
ddae0ab
Add metadata information to unsize coercions
Nadrieril Jul 10, 2025
d6d523d
Merge branch 'unsize-metadata' into dev
Nadrieril Jul 10, 2025
88346de
Never construct `ItemRef`s by hand
Nadrieril Jul 10, 2025
25795fa
resolution: let `PredicateSearcher` index clauses itself
Nadrieril Jul 10, 2025
fac3d69
Resolve `dyn` projection predicates
Nadrieril Jul 10, 2025
d30960b
Merge branch 'resolve-dyn' into dev
Nadrieril Jul 11, 2025
3972a6c
Fix `import_thir` after the `dyn` PR
Nadrieril Jul 11, 2025
9641a29
full_def: inline `get_ctor_contents`
Nadrieril Jul 8, 2025
a6be3d1
full_def: merge the three ADT types
Nadrieril Jul 8, 2025
00f4cf2
full_def: merge `AdtDef` into `FullDefKind`
Nadrieril Jul 8, 2025
83cc3e4
full_def: merge the various kinds of constant items
Nadrieril Jul 8, 2025
d4762a3
full_def: remove `parent` fields
Nadrieril Jul 10, 2025
2d7e20f
full_def: move heavy rustc logic to `rustc_utils`
Nadrieril Jul 10, 2025
9d12de0
full_def: add helper
Nadrieril Jul 10, 2025
e91aedb
full_def: better way to translate type aliases
Nadrieril Jul 10, 2025
32a73b4
Merge branch 'fulldef-cleanup' into dev
Nadrieril Jul 17, 2025
e837a1a
full_def: provide virtual impl for `Drop`
Nadrieril Jul 18, 2025
f3a3e1c
Tweak the representation of special `ImplExpr`s for `Drop`
Nadrieril Jul 18, 2025
700932c
Merge branch 'improve-drop-impl' into dev
Nadrieril Jul 21, 2025
773fc14
Fix equality on `Ty`
Nadrieril Jul 19, 2025
0af2fc5
full_def: don't store entire `FullDef`s in assoc items
Nadrieril Jul 17, 2025
f98a7c8
Implement `SInto` for `AssocItem` manually
Nadrieril Jul 11, 2025
60d847f
Allow recovering the `ty::GenericArgs` from an `ItemRef`
Nadrieril Jul 10, 2025
7bfa499
full_def: allow instantiating `FullDef` with generics
Nadrieril Jul 11, 2025
040d9a9
full_def: store a self-reference in `FullDef`
Nadrieril Jul 18, 2025
7d1c872
full_def: track parent item with appropriate args
Nadrieril Jul 19, 2025
b2d6177
full_def: cache instantiated translation
Nadrieril Jul 18, 2025
edbffed
Translate arguments of implemented item in `AssocItem`
Nadrieril Jul 18, 2025
f46482c
Normalize after substituting
Nadrieril Jul 19, 2025
97c713f
ItemRef: add erasure capabilities
Nadrieril Jul 21, 2025
8eded27
Merge branch 'mono-fulldef' into dev
Nadrieril Jul 21, 2025
6776d63
Merge remote-tracking branch 'upstream/main' into upstream-pr
Nadrieril Jul 22, 2025
60c354f
Commit test outputs
Nadrieril Jul 22, 2025
2864c04
Merge remote-tracking branch 'origin/main' into nadrieril-upstream-pr
W95Psp Jul 28, 2025
b2a2a29
fix(rengine): respect env var `HAX_ENGINE_BINARY`
W95Psp Jul 28, 2025
4952add
chore: refresh tests
W95Psp Jul 28, 2025
f68b3ab
chore: update rustc-coverage-tests
W95Psp Jul 28, 2025
108105f
fix: typo
Nadrieril Jul 29, 2025
501a8aa
Merge branch 'main' into upstream-pr
W95Psp Sep 4, 2025
d7f6869
Merge remote-tracking branch 'origin/main' into nadrieril-upstream-pr
W95Psp Sep 9, 2025
18727c3
chore: update changlelog
W95Psp Sep 9, 2025
1df28c4
fix(engine): rewrite `Self`
W95Psp Jul 28, 2025
3a54634
chore: update test snapshots
W95Psp Sep 9, 2025
44f041c
Merge branch 'main' into upstream-pr
W95Psp Sep 9, 2025
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
18 changes: 11 additions & 7 deletions engine/lib/import_thir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1082,21 +1082,25 @@ end) : EXPR = struct
| Error ->
assertion_failure [ span ]
"got type `Error`: Rust compilation probably failed."
| Dynamic (predicates, _region, Dyn) -> (
| Dynamic (_, predicates, _region) -> (
let goals, non_traits =
List.partition_map
~f:(fun pred ->
match pred.value with
| Trait { args; def_id } ->
~f:(fun ((clause, _span) : Types.clause * _) ->
match clause.kind.value with
| Trait { trait_ref; _ } ->
let goal : dyn_trait_goal =
{
trait = Concrete_ident.of_def_id ~value:false def_id;
non_self_args = List.map ~f:(c_generic_value span) args;
trait =
Concrete_ident.of_def_id ~value:false
trait_ref.value.def_id;
non_self_args =
List.map ~f:(c_generic_value span)
(List.tl_exn trait_ref.value.generic_args);
}
in
First goal
| _ -> Second ())
predicates
predicates.predicates
in
match non_traits with
| [] -> TDyn { witness = W.dyn; goals }
Expand Down
196 changes: 195 additions & 1 deletion frontend/exporter/src/rustc_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;
use rustc_middle::ty;
use rustc_hir::def::DefKind as RDefKind;
use rustc_middle::{mir, ty};

#[extension_traits::extension(pub trait SubstBinder)]
impl<'tcx, T: ty::TypeFoldable<ty::TyCtxt<'tcx>>> ty::Binder<'tcx, T> {
Expand Down Expand Up @@ -114,3 +115,196 @@ pub fn get_closest_parent_type(
_ => get_closest_parent_type(tcx, tcx.parent(id)),
}
}

/// Gets the visibility (`pub` or not) of the definition. Returns `None` for defs that don't have a
/// meaningful visibility.
pub fn get_def_visibility<'tcx>(
tcx: ty::TyCtxt<'tcx>,
def_id: RDefId,
def_kind: RDefKind,
) -> Option<bool> {
use RDefKind::*;
match def_kind {
AssocConst
| AssocFn
| Const
| Enum
| Field
| Fn
| ForeignTy
| Macro { .. }
| Mod
| Static { .. }
| Struct
| Trait
| TraitAlias
| TyAlias { .. }
| Union
| Use
| Variant => Some(tcx.visibility(def_id).is_public()),
// These kinds don't have visibility modifiers (which would cause `visibility` to panic).
AnonConst
| AssocTy
| Closure
| ConstParam
| Ctor { .. }
| ExternCrate
| ForeignMod
| GlobalAsm
| Impl { .. }
| InlineConst
| LifetimeParam
| OpaqueTy
| SyntheticCoroutineBody
| TyParam => None,
}
}

/// Gets the attributes of the definition.
pub fn get_def_attrs<'tcx>(
tcx: ty::TyCtxt<'tcx>,
def_id: RDefId,
def_kind: RDefKind,
) -> &'tcx [rustc_hir::Attribute] {
use RDefKind::*;
match def_kind {
// These kinds cause `get_attrs_unchecked` to panic.
ConstParam | LifetimeParam | TyParam | ForeignMod => &[],
_ => tcx.get_attrs_unchecked(def_id),
}
}

/// Gets the children of a module.
pub fn get_mod_children<'tcx>(
tcx: ty::TyCtxt<'tcx>,
def_id: RDefId,
) -> Vec<(Option<rustc_span::Ident>, RDefId)> {
match def_id.as_local() {
Some(ldid) => match tcx.hir_node_by_def_id(ldid) {
rustc_hir::Node::Crate(m)
| rustc_hir::Node::Item(&rustc_hir::Item {
kind: rustc_hir::ItemKind::Mod(_, m),
..
}) => m
.item_ids
.iter()
.map(|&item_id| {
let opt_ident = tcx.hir_item(item_id).kind.ident();
let def_id = item_id.owner_id.to_def_id();
(opt_ident, def_id)
})
.collect(),
node => panic!("DefKind::Module is an unexpected node: {node:?}"),
},
None => tcx
.module_children(def_id)
.iter()
.map(|child| (Some(child.ident), child.res.def_id()))
.collect(),
}
}

/// Gets the children of an `extern` block. Empty if the block is not defined in the current crate.
pub fn get_foreign_mod_children<'tcx>(tcx: ty::TyCtxt<'tcx>, def_id: RDefId) -> Vec<RDefId> {
match def_id.as_local() {
Some(ldid) => tcx
.hir_node_by_def_id(ldid)
.expect_item()
.expect_foreign_mod()
.1
.iter()
.map(|foreign_item_ref| foreign_item_ref.id.owner_id.to_def_id())
.collect(),
None => vec![],
}
}

/// The signature of a method impl may be a subtype of the one expected from the trait decl, as in
/// the example below. For correctness, we must be able to map from the method generics declared in
/// the trait to the actual method generics. Because this would require type inference, we instead
/// simply return the declared signature. This will cause issues if it is possible to use such a
/// more-specific implementation with its more-specific type, but we have a few other issues with
/// lifetime-generic function pointers anyway so this is unlikely to cause problems.
///
/// ```ignore
/// trait MyCompare<Other>: Sized {
/// fn compare(self, other: Other) -> bool;
/// }
/// impl<'a> MyCompare<&'a ()> for &'a () {
/// // This implementation is more general because it works for non-`'a` refs. Note that only
/// // late-bound vars may differ in this way.
/// // `<&'a () as MyCompare<&'a ()>>::compare` has type `fn<'b>(&'a (), &'b ()) -> bool`,
/// // but type `fn(&'a (), &'a ()) -> bool` was expected from the trait declaration.
/// fn compare<'b>(self, _other: &'b ()) -> bool {
/// true
/// }
/// }
/// ```
pub fn get_method_sig<'tcx>(tcx: ty::TyCtxt<'tcx>, def_id: RDefId) -> ty::PolyFnSig<'tcx> {
let real_sig = tcx.fn_sig(def_id).instantiate_identity();
let item = tcx.associated_item(def_id);
if !matches!(item.container, ty::AssocItemContainer::Impl) {
return real_sig;
}
let Some(decl_method_id) = item.trait_item_def_id else {
return real_sig;
};
let declared_sig = tcx.fn_sig(decl_method_id);

// TODO(Nadrieril): Temporary hack: if the signatures have the same number of bound vars, we
// keep the real signature. While the declared signature is more correct, it is also less
// normalized and we can't normalize without erasing regions but regions are crucial in
// function signatures. Hence we cheat here, until charon gains proper normalization
// capabilities.
if declared_sig.skip_binder().bound_vars().len() == real_sig.bound_vars().len() {
return real_sig;
}

let impl_def_id = item.container_id(tcx);
// The trait predicate that is implemented by the surrounding impl block.
let implemented_trait_ref = tcx
.impl_trait_ref(impl_def_id)
.unwrap()
.instantiate_identity();
// Construct arguments for the declared method generics in the context of the implemented
// method generics.
let impl_args = ty::GenericArgs::identity_for_item(tcx, def_id);
let decl_args = impl_args.rebase_onto(tcx, impl_def_id, implemented_trait_ref.args);
let sig = declared_sig.instantiate(tcx, decl_args);
// Avoids accidentally using the same lifetime name twice in the same scope
// (once in impl parameters, second in the method declaration late-bound vars).
let sig = tcx.anonymize_bound_vars(sig);
sig
}

pub fn closure_once_shim<'tcx>(
tcx: ty::TyCtxt<'tcx>,
closure_ty: ty::Ty<'tcx>,
) -> Option<mir::Body<'tcx>> {
let ty::Closure(def_id, args) = closure_ty.kind() else {
unreachable!()
};
let instance = match args.as_closure().kind() {
ty::ClosureKind::Fn | ty::ClosureKind::FnMut => {
ty::Instance::fn_once_adapter_instance(tcx, *def_id, args)
}
ty::ClosureKind::FnOnce => return None,
};
let mir = tcx.instance_mir(instance.def).clone();
let mir = ty::EarlyBinder::bind(mir).instantiate(tcx, instance.args);
Some(mir)
}

pub fn drop_glue_shim<'tcx>(tcx: ty::TyCtxt<'tcx>, def_id: RDefId) -> Option<mir::Body<'tcx>> {
let drop_in_place =
tcx.require_lang_item(rustc_hir::LangItem::DropInPlace, rustc_span::DUMMY_SP);
if !tcx.generics_of(def_id).is_empty() {
// Hack: layout code panics if it can't fully normalize types, which can happen e.g. with a
// trait associated type. For now we only translate the glue for monomorphic types.
return None;
}
let ty = tcx.type_of(def_id).instantiate_identity();
let instance_kind = ty::InstanceKind::DropGlue(drop_in_place, Some(ty));
let mir = tcx.instance_mir(instance_kind).clone();
Some(mir)
}
Loading