Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 73a4208

Browse files
Rollup merge of rust-lang#123176 - celinval:smir-field-ty, r=oli-obk
Normalize the result of `Fields::ty_with_args` We were only instantiating before, which would leak an AliasTy. I added a test case that reproduce the issue seen here: model-checking/kani#3113 r? ``@oli-obk``
2 parents 45796d1 + a325bce commit 73a4208

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

‎compiler/rustc_smir/src/rustc_smir/context.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
420420
let tcx = tables.tcx;
421421
let args = args.internal(&mut *tables, tcx);
422422
let def_ty = tables.tcx.type_of(item.internal(&mut *tables, tcx));
423-
def_ty.instantiate(tables.tcx, args).stable(&mut *tables)
423+
tables
424+
.tcx
425+
.instantiate_and_normalize_erasing_regions(args, ty::ParamEnv::reveal_all(), def_ty)
426+
.stable(&mut *tables)
424427
}
425428

426429
fn const_pretty(&self, cnst: &stable_mir::ty::Const) -> String {

‎compiler/stable_mir/src/ty.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl AdtDef {
654654
with(|cx| cx.def_ty(self.0))
655655
}
656656

657-
/// Retrieve the type of this Adt instantiating the type with the given arguments.
657+
/// Retrieve the type of this Adt by instantiating and normalizing it with the given arguments.
658658
///
659659
/// This will assume the type can be instantiated with these arguments.
660660
pub fn ty_with_args(&self, args: &GenericArgs) -> Ty {
@@ -733,7 +733,7 @@ pub struct FieldDef {
733733
}
734734

735735
impl FieldDef {
736-
/// Retrieve the type of this field instantiating the type with the given arguments.
736+
/// Retrieve the type of this field instantiating and normalizing it with the given arguments.
737737
///
738738
/// This will assume the type can be instantiated with these arguments.
739739
pub fn ty_with_args(&self, args: &GenericArgs) -> Ty {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//@ run-pass
2+
//! Test that types are normalized in an instance body.
3+
4+
//@ ignore-stage1
5+
//@ ignore-cross-compile
6+
//@ ignore-remote
7+
//@ ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
8+
//@ edition: 2021
9+
10+
#![feature(rustc_private)]
11+
12+
#[macro_use]
13+
extern crate rustc_smir;
14+
extern crate rustc_driver;
15+
extern crate rustc_interface;
16+
extern crate stable_mir;
17+
18+
use mir::mono::Instance;
19+
use ty::{Ty, TyKind, RigidTy};
20+
use rustc_smir::rustc_internal;
21+
use stable_mir::*;
22+
use std::io::Write;
23+
use std::ops::ControlFlow;
24+
25+
const CRATE_NAME: &str = "input";
26+
27+
/// This function uses the Stable MIR APIs to get information about the test crate.
28+
fn test_stable_mir() -> ControlFlow<()> {
29+
let items = stable_mir::all_local_items();
30+
31+
// Get all items and split generic vs monomorphic items.
32+
let instances: Vec<_> =
33+
items.into_iter().filter_map(|item| (!item.requires_monomorphization()).then(|| {
34+
Instance::try_from(item).unwrap()
35+
})).collect();
36+
assert_eq!(instances.len(), 1, "Expected one constant");
37+
38+
for instance in instances {
39+
check_ty(instance.ty());
40+
}
41+
ControlFlow::Continue(())
42+
}
43+
44+
fn check_ty(ty: Ty) {
45+
match ty.kind() {
46+
TyKind::RigidTy(RigidTy::Adt(def, args)) if def.kind().is_struct() => {
47+
// Ensure field type is also normalized
48+
def.variants_iter().next().unwrap().fields().into_iter().for_each(|f| {
49+
check_ty(f.ty_with_args(&args))
50+
});
51+
}
52+
TyKind::RigidTy(RigidTy::Uint(..)) => {}
53+
kind => unreachable!("Unexpected kind: {kind:?}")
54+
}
55+
}
56+
57+
58+
/// This test will generate and analyze a dummy crate using the stable mir.
59+
/// For that, it will first write the dummy crate into a file.
60+
/// Then it will create a `StableMir` using custom arguments and then
61+
/// it will run the compiler.
62+
fn main() {
63+
let path = "normalization_input.rs";
64+
generate_input(&path).unwrap();
65+
let args = vec![
66+
"rustc".to_string(),
67+
"-Cpanic=abort".to_string(),
68+
"--crate-type=lib".to_string(),
69+
"--crate-name".to_string(),
70+
CRATE_NAME.to_string(),
71+
path.to_string(),
72+
];
73+
run!(args, test_stable_mir).unwrap();
74+
}
75+
76+
fn generate_input(path: &str) -> std::io::Result<()> {
77+
let mut file = std::fs::File::create(path)?;
78+
write!(
79+
file,
80+
r#"
81+
pub trait Primitive {{
82+
type Base;
83+
}}
84+
85+
impl Primitive for char {{
86+
type Base = u32;
87+
}}
88+
89+
pub struct Wrapper<T: Primitive>(T::Base);
90+
pub type WrapperChar = Wrapper<char>;
91+
pub const NULL_CHAR: WrapperChar = Wrapper::<char>(0);
92+
"#
93+
)?;
94+
Ok(())
95+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /