Skip to content
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
13 changes: 13 additions & 0 deletions src/analyze/basic_block.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -920,12 +920,25 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
}
}

/// Analyzes the body of the implementation a call dispatches to.
///
/// A call typed by a trait method's spec never asks for the implementing method's type, and
/// an implementation with generic parameters is analyzed only when its type is asked for.
fn analyze_resolved_impl_body(&mut self, def_id: DefId, args: mir_ty::GenericArgsRef<'tcx>) {
let (impl_def_id, impl_args) = self.resolve_fn_def(def_id, args);
if impl_def_id == def_id {
return;
}
let _ = self.ctx.def_ty_with_args(impl_def_id, impl_args);
}

fn fn_def_ty(
&mut self,
def_id: DefId,
args: mir_ty::GenericArgsRef<'tcx>,
) -> rty::Type<rty::Closed> {
if let Some(def_ty) = self.ctx.def_ty_with_args(def_id, args) {
self.analyze_resolved_impl_body(def_id, args);
return def_ty.ty;
}

Expand Down
9 changes: 7 additions & 2 deletions src/analyze/local_def.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -45,6 +45,7 @@ pub struct Analyzer<'tcx, 'ctx> {
local_def_id: LocalDefId,

body: Body<'tcx>,
/// the instantiation of the def's generic parameters being analyzed, also used
/// to substitute HIR types during translation in [`crate::analyze::annot_fn`]
generic_args: mir_ty::GenericArgsRef<'tcx>,
drop_points: HashMap<BasicBlock, analyze::basic_block::DropPoints>,
Expand DownExpand Up@@ -198,7 +199,11 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
return None;
}

let trait_ref = self.tcx.impl_trait_ref(impl_did)?.instantiate_identity();
// an associated method's generic arguments start with those of its impl
let trait_ref = self
.tcx
.impl_trait_ref(impl_did)?
.instantiate(self.tcx, self.generic_args);
let trait_item_did = self
.tcx
.associated_item(self.local_def_id.to_def_id())
Expand DownExpand Up@@ -1115,7 +1120,7 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
let body = tcx.optimized_mir(local_def_id.to_def_id()).clone();
let drop_points = Default::default();
let type_builder = TypeBuilder::new(tcx, ctx.def_ids(), local_def_id.to_def_id());
let generic_args = tcx.mk_args(&[]);
let generic_args = mir_ty::GenericArgs::identity_for_item(tcx, local_def_id);
Self {
ctx,
tcx,
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/fail/trait_generic_impl.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
//@error-in-other-file: Unsat
//@compile-flags: -C debug-assertions=off

#[thrust_macros::context]
trait Tr {
#[thrust_macros::requires(true)]
#[thrust_macros::ensures(result > 0)]
fn m(&self) -> i32;
}

struct W<T>(T);

impl<T> thrust_models::Model for W<T>
where
T: thrust_models::Model,
{
type Ty = Self;
}

impl<T> Tr for W<T> {
fn m(&self) -> i32 {
-1
}
}

fn main() {
let w = W(0i32);
assert!(w.m() > 0);
}
29 changes: 29 additions & 0 deletions tests/ui/pass/trait_generic_impl.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
//@check-pass
//@compile-flags: -C debug-assertions=off

#[thrust_macros::context]
trait Tr {
#[thrust_macros::requires(true)]
#[thrust_macros::ensures(result > 0)]
fn m(&self) -> i32;
}

struct W<T>(T);

impl<T> thrust_models::Model for W<T>
where
T: thrust_models::Model,
{
type Ty = Self;
}

impl<T> Tr for W<T> {
fn m(&self) -> i32 {
1
}
}

fn main() {
let w = W(0i32);
assert!(w.m() > 0);
}