Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8f9c7df

Browse files
dianqkcuviper
authored andcommitted
Don't infer attributes of virtual calls based on the function body
(cherry picked from commit 8089fce)
1 parent 98ec247 commit 8f9c7df

File tree

5 files changed

+52
-60
lines changed

5 files changed

+52
-60
lines changed

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ pub enum InstanceKind<'tcx> {
111111

112112
/// Dynamic dispatch to `<dyn Trait as Trait>::fn`.
113113
///
114-
/// This `InstanceKind` does not have callable MIR. Calls to `Virtual` instances must be
115-
/// codegen'd as virtual calls through the vtable.
114+
/// This `InstanceKind` may have a callable MIR as the default implementation.
115+
/// Calls to `Virtual` instances must be codegen'd as virtual calls through the vtable.
116+
/// *This means we might not know exactly what is being called.*
116117
///
117118
/// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more
118119
/// details on that).

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,11 @@ fn fn_abi_of_fn_ptr<'tcx>(
309309
query: ty::PseudoCanonicalInput<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
310310
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
311311
let ty::PseudoCanonicalInput { typing_env, value: (sig, extra_args) } = query;
312-
313-
let cx = LayoutCx::new(tcx, typing_env);
314312
fn_abi_new_uncached(
315-
&cx,
313+
&LayoutCx::new(tcx, typing_env),
316314
tcx.instantiate_bound_regions_with_erased(sig),
317315
extra_args,
318316
None,
319-
None,
320-
false,
321317
)
322318
}
323319

@@ -326,19 +322,11 @@ fn fn_abi_of_instance<'tcx>(
326322
query: ty::PseudoCanonicalInput<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
327323
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
328324
let ty::PseudoCanonicalInput { typing_env, value: (instance, extra_args) } = query;
329-
330-
let sig = fn_sig_for_fn_abi(tcx, instance, typing_env);
331-
332-
let caller_location =
333-
instance.def.requires_caller_location(tcx).then(|| tcx.caller_location_ty());
334-
335325
fn_abi_new_uncached(
336326
&LayoutCx::new(tcx, typing_env),
337-
sig,
327+
fn_sig_for_fn_abi(tcx, instance, typing_env),
338328
extra_args,
339-
caller_location,
340-
Some(instance.def_id()),
341-
matches!(instance.def, ty::InstanceKind::Virtual(..)),
329+
Some(instance),
342330
)
343331
}
344332

@@ -549,19 +537,23 @@ fn fn_abi_sanity_check<'tcx>(
549537
fn_arg_sanity_check(cx, fn_abi, spec_abi, &fn_abi.ret);
550538
}
551539

552-
// FIXME(eddyb) perhaps group the signature/type-containing (or all of them?)
553-
// arguments of this method, into a separate `struct`.
554-
#[tracing::instrument(level = "debug", skip(cx, caller_location, fn_def_id, force_thin_self_ptr))]
540+
#[tracing::instrument(level = "debug", skip(cx, instance))]
555541
fn fn_abi_new_uncached<'tcx>(
556542
cx: &LayoutCx<'tcx>,
557543
sig: ty::FnSig<'tcx>,
558544
extra_args: &[Ty<'tcx>],
559-
caller_location: Option<Ty<'tcx>>,
560-
fn_def_id: Option<DefId>,
561-
// FIXME(eddyb) replace this with something typed, like an `enum`.
562-
force_thin_self_ptr: bool,
545+
instance: Option<ty::Instance<'tcx>>,
563546
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
564547
let tcx = cx.tcx();
548+
let (caller_location, fn_def_id, is_virtual_call) = if let Some(instance) = instance {
549+
(
550+
instance.def.requires_caller_location(tcx).then(|| tcx.caller_location_ty()),
551+
Some(instance.def_id()),
552+
matches!(instance.def, ty::InstanceKind::Virtual(..)),
553+
)
554+
} else {
555+
(None, None, false)
556+
};
565557
let sig = tcx.normalize_erasing_regions(cx.typing_env, sig);
566558

567559
let conv = conv_from_spec_abi(cx.tcx(), sig.abi, sig.c_variadic);
@@ -570,16 +562,11 @@ fn fn_abi_new_uncached<'tcx>(
570562
let extra_args = if sig.abi == ExternAbi::RustCall {
571563
assert!(!sig.c_variadic && extra_args.is_empty());
572564

573-
if let Some(input) = sig.inputs().last() {
574-
if let ty::Tuple(tupled_arguments) = input.kind() {
575-
inputs = &sig.inputs()[0..sig.inputs().len() - 1];
576-
tupled_arguments
577-
} else {
578-
bug!(
579-
"argument to function with \"rust-call\" ABI \
580-
is not a tuple"
581-
);
582-
}
565+
if let Some(input) = sig.inputs().last()
566+
&& let ty::Tuple(tupled_arguments) = input.kind()
567+
{
568+
inputs = &sig.inputs()[0..sig.inputs().len() - 1];
569+
tupled_arguments
583570
} else {
584571
bug!(
585572
"argument to function with \"rust-call\" ABI \
@@ -605,7 +592,7 @@ fn fn_abi_new_uncached<'tcx>(
605592
});
606593

607594
let layout = cx.layout_of(ty).map_err(|err| &*tcx.arena.alloc(FnAbiError::Layout(*err)))?;
608-
let layout = if force_thin_self_ptr && arg_idx == Some(0) {
595+
let layout = if is_virtual_call && arg_idx == Some(0) {
609596
// Don't pass the vtable, it's not an argument of the virtual fn.
610597
// Instead, pass just the data pointer, but give it the type `*const/mut dyn Trait`
611598
// or `&/&mut dyn Trait` because this is special-cased elsewhere in codegen
@@ -650,7 +637,15 @@ fn fn_abi_new_uncached<'tcx>(
650637
conv,
651638
can_unwind: fn_can_unwind(cx.tcx(), fn_def_id, sig.abi),
652639
};
653-
fn_abi_adjust_for_abi(cx, &mut fn_abi, sig.abi, fn_def_id);
640+
fn_abi_adjust_for_abi(
641+
cx,
642+
&mut fn_abi,
643+
sig.abi,
644+
// If this is a virtual call, we cannot pass the `fn_def_id`, as it might call other
645+
// functions from vtable. Internally, `deduced_param_attrs` attempts to infer attributes by
646+
// visit the function body.
647+
fn_def_id.filter(|_| !is_virtual_call),
648+
);
654649
debug!("fn_abi_new_uncached = {:?}", fn_abi);
655650
fn_abi_sanity_check(cx, &fn_abi, sig.abi);
656651
Ok(tcx.arena.alloc(fn_abi))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Regression test for https://github.com/rust-lang/rust/issues/137646.
2+
//! Since we don't know the exact implementation of the virtual call,
3+
//! it might write to parameters, we can't infer the readonly attribute.
4+
//@ compile-flags: -C opt-level=3 -C no-prepopulate-passes
5+
6+
#![crate_type = "lib"]
7+
8+
pub trait Trait {
9+
fn m(&self, _: (i32, i32, i32)) {}
10+
}
11+
12+
#[no_mangle]
13+
pub fn foo(trait_: &dyn Trait) {
14+
// CHECK-LABEL: @foo(
15+
// CHECK: call void
16+
// CHECK-NOT: readonly
17+
trait_.m((1, 1, 1));
18+
}

tests/ui/virtual-call-attrs-issue-137646.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Regression test for https://github.com/rust-lang/rust/issues/137646.
2-
//! FIXME: The parameter value at all calls to `check` should be `(1, 1, 1)`.
2+
//! The parameter value at all calls to `check` should be `(1, 1, 1)`.
33
44
//@ run-pass
5-
//@ check-run-results
65

76
use std::hint::black_box;
87

@@ -35,8 +34,7 @@ pub fn run_2(trait_: &dyn Trait) {
3534

3635
#[inline(never)]
3736
fn check(v: T) {
38-
dbg!(v);
39-
// assert_eq!(v, (1, 1, 1));
37+
assert_eq!(v, (1, 1, 1));
4038
}
4139

4240
fn main() {

tests/ui/virtual-call-attrs-issue-137646.run.stderr

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)