From ec188115578db5207ba84977d7314a58e7d117e1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 21 Sep 2023 13:58:24 +0200 Subject: [PATCH 01/19] run abi/compatibility test against a whole bunch of targets --- tests/ui/abi/compatibility.rs | 183 ++++++++++++++++++++++++++++++++-- 1 file changed, 173 insertions(+), 10 deletions(-) diff --git a/tests/ui/abi/compatibility.rs b/tests/ui/abi/compatibility.rs index 249e817628303..1f049b1785a62 100644 --- a/tests/ui/abi/compatibility.rs +++ b/tests/ui/abi/compatibility.rs @@ -1,16 +1,174 @@ // check-pass +// revisions: host +// revisions: arm +//[arm] compile-flags: --target arm-unknown-linux-gnueabi +//[arm] needs-llvm-components: arm +// revisions: aarch64 +//[aarch64] compile-flags: --target aarch64-unknown-linux-gnu +//[aarch64] needs-llvm-components: aarch64 +// revisions: s390x +//[s390x] compile-flags: --target s390x-unknown-linux-gnu +//[s390x] needs-llvm-components: systemz +// revisions: mips +//[mips] compile-flags: --target mips-unknown-linux-gnu +//[mips] needs-llvm-components: mips +// revisions: mips64 +//[mips64] compile-flags: --target mips64-unknown-linux-gnuabi64 +//[mips64] needs-llvm-components: mips +// revisions: sparc +//[sparc] compile-flags: --target sparc-unknown-linux-gnu +//[sparc] needs-llvm-components: sparc +// revisions: sparc64 +//[sparc64] compile-flags: --target sparc64-unknown-linux-gnu +//[sparc64] needs-llvm-components: sparc +// revisions: powerpc64 +//[powerpc64] compile-flags: --target powerpc64-unknown-linux-gnu +//[powerpc64] needs-llvm-components: powerpc +// revisions: riscv +//[riscv] compile-flags: --target riscv64gc-unknown-linux-gnu +//[riscv] needs-llvm-components: riscv +// revisions: loongarch64 +//[loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu +//[loongarch64] needs-llvm-components: loongarch +// revisions: wasm +//[wasm] compile-flags: --target wasm32-unknown-unknown +//[wasm] needs-llvm-components: webassembly +// revisions: wasi +//[wasi] compile-flags: --target wasm32-wasi +//[wasi] needs-llvm-components: webassembly +// revisions: nvptx64 +//[nvptx64] compile-flags: --target nvptx64-nvidia-cuda +//[nvptx64] needs-llvm-components: nvptx #![feature(rustc_attrs, unsized_fn_params, transparent_unions)] +#![cfg_attr(not(host), feature(no_core, lang_items), no_std, no_core)] #![allow(unused, improper_ctypes_definitions, internal_features)] -use std::marker::PhantomData; -use std::mem::ManuallyDrop; -use std::num::NonZeroI32; -use std::ptr::NonNull; -// FIXME: a bunch of targets are broken in various ways. +// FIXME: some targets are broken in various ways. // Hence there are `cfg` throughout this test to disable parts of it on those targets. // sparc64: https://github.com/rust-lang/rust/issues/115336 // mips64: https://github.com/rust-lang/rust/issues/115404 +#[cfg(host)] +use std::{ + any::Any, marker::PhantomData, mem::ManuallyDrop, num::NonZeroI32, ptr::NonNull, rc::Rc, + sync::Arc, +}; + +/// To work cross-target this test must be no_core. +/// This little prelude supplies what we need. +#[cfg(not(host))] +mod prelude { + #[lang = "sized"] + pub trait Sized {} + + #[lang = "receiver"] + pub trait Receiver {} + impl Receiver for &T {} + impl Receiver for &mut T {} + + #[lang = "copy"] + pub trait Copy: Sized {} + impl Copy for i32 {} + impl Copy for f32 {} + impl Copy for &T {} + impl Copy for *const T {} + impl Copy for *mut T {} + + #[lang = "clone"] + pub trait Clone: Sized { + fn clone(&self) -> Self; + } + + #[lang = "phantom_data"] + pub struct PhantomData; + impl Copy for PhantomData {} + + #[lang = "unsafe_cell"] + #[repr(transparent)] + pub struct UnsafeCell { + value: T, + } + + pub trait Any: 'static {} + + pub enum Option { + None, + Some(T), + } + impl Copy for Option {} + + pub enum Result { + Ok(T), + Err(E), + } + impl Copy for Result {} + + #[lang = "manually_drop"] + #[repr(transparent)] + pub struct ManuallyDrop { + value: T, + } + impl Copy for ManuallyDrop {} + + #[repr(transparent)] + #[rustc_layout_scalar_valid_range_start(1)] + #[rustc_nonnull_optimization_guaranteed] + pub struct NonNull { + pointer: *const T, + } + impl Copy for NonNull {} + + #[repr(transparent)] + #[rustc_layout_scalar_valid_range_start(1)] + #[rustc_nonnull_optimization_guaranteed] + pub struct NonZeroI32(i32); + + // This just stands in for a non-trivial type. + pub struct Vec { + ptr: NonNull, + cap: usize, + len: usize, + } + + pub struct Unique { + pub pointer: NonNull, + pub _marker: PhantomData, + } + + pub struct Global; + + #[lang = "owned_box"] + pub struct Box(Unique, A); + + #[repr(C)] + struct RcBox { + strong: UnsafeCell, + weak: UnsafeCell, + value: T, + } + pub struct Rc { + ptr: NonNull>, + phantom: PhantomData>, + alloc: A, + } + + #[repr(C, align(8))] + struct AtomicUsize(usize); + #[repr(C)] + struct ArcInner { + strong: AtomicUsize, + weak: AtomicUsize, + data: T, + } + pub struct Arc { + ptr: NonNull>, + phantom: PhantomData>, + alloc: A, + } +} +#[cfg(not(host))] +use prelude::*; + macro_rules! assert_abi_compatible { ($name:ident, $t1:ty, $t2:ty) => { mod $name { @@ -26,8 +184,13 @@ macro_rules! assert_abi_compatible { }; } -#[derive(Copy, Clone)] struct Zst; +impl Copy for Zst {} +impl Clone for Zst { + fn clone(&self) -> Self { + Zst + } +} #[repr(C)] struct ReprC1(T); @@ -85,8 +248,8 @@ test_abi_compatible!(nonzero_int, NonZeroI32, i32); // `DispatchFromDyn` relies on ABI compatibility. // This is interesting since these types are not `repr(transparent)`. -test_abi_compatible!(rc, std::rc::Rc, *mut i32); -test_abi_compatible!(arc, std::sync::Arc, *mut i32); +test_abi_compatible!(rc, Rc, *mut i32); +test_abi_compatible!(arc, Arc, *mut i32); // `repr(transparent)` compatibility. #[repr(transparent)] @@ -160,7 +323,7 @@ mod unsized_ { use super::*; test_transparent_unsized!(str_, str); test_transparent_unsized!(slice, [u8]); - test_transparent_unsized!(dyn_trait, dyn std::any::Any); + test_transparent_unsized!(dyn_trait, dyn Any); } // RFC 3391 . @@ -185,7 +348,7 @@ test_nonnull!(ref_unsized, &[i32]); test_nonnull!(mut_unsized, &mut [i32]); test_nonnull!(fn_, fn()); test_nonnull!(nonnull, NonNull); -test_nonnull!(nonnull_unsized, NonNull); +test_nonnull!(nonnull_unsized, NonNull); test_nonnull!(non_zero, NonZeroI32); fn main() {} From f99fdac3df5f97287857786bcce9660250009cbf Mon Sep 17 00:00:00 2001 From: WANG Rui Date: Thu, 28 Sep 2023 10:09:53 +0800 Subject: [PATCH 02/19] ci: upgrade to crosstool-ng 1.26.0 --- src/ci/docker/README.md | 4 ++-- .../dist-loongarch64-linux/Dockerfile | 5 ++--- src/ci/docker/scripts/crosstool-ng-git.sh | 17 ----------------- src/ci/docker/scripts/crosstool-ng.sh | 2 +- 4 files changed, 5 insertions(+), 23 deletions(-) delete mode 100644 src/ci/docker/scripts/crosstool-ng-git.sh diff --git a/src/ci/docker/README.md b/src/ci/docker/README.md index b83b198780ba7..2e64568371306 100644 --- a/src/ci/docker/README.md +++ b/src/ci/docker/README.md @@ -271,7 +271,7 @@ For targets: `loongarch64-unknown-linux-gnu` - Operating System > Linux kernel version = 5.19.16 - Binary utilities > Version of binutils = 2.40 - C-library > glibc version = 2.36 -- C compiler > gcc version = 13.1.0 +- C compiler > gcc version = 13.2.0 - C compiler > C++ = ENABLE -- to cross compile LLVM ### `mips-linux-gnu.defconfig` @@ -407,7 +407,7 @@ For targets: `riscv64-unknown-linux-gnu` - Target options > Bitness = 64-bit - Operating System > Target OS = linux - Operating System > Linux kernel version = 4.20.17 -- Binary utilities > Version of binutils = 2.32 +- Binary utilities > Version of binutils = 2.36.1 - C-library > glibc version = 2.29 - C compiler > gcc version = 8.5.0 - C compiler > C++ = ENABLE -- to cross compile LLVM diff --git a/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile b/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile index 78689c429c2e3..55c737bd0aa31 100644 --- a/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile @@ -3,9 +3,8 @@ FROM ubuntu:22.04 COPY scripts/cross-apt-packages.sh /scripts/ RUN sh /scripts/cross-apt-packages.sh -# The latest released version does not support LoongArch. -COPY scripts/crosstool-ng-git.sh /scripts/ -RUN sh /scripts/crosstool-ng-git.sh +COPY scripts/crosstool-ng.sh /scripts/ +RUN sh /scripts/crosstool-ng.sh COPY scripts/rustbuild-setup.sh /scripts/ RUN sh /scripts/rustbuild-setup.sh diff --git a/src/ci/docker/scripts/crosstool-ng-git.sh b/src/ci/docker/scripts/crosstool-ng-git.sh deleted file mode 100644 index b8d3991532717..0000000000000 --- a/src/ci/docker/scripts/crosstool-ng-git.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -set -ex - -URL=https://github.com/crosstool-ng/crosstool-ng -REV=227d99d7f3115f3a078595a580d2b307dcd23e93 - -mkdir crosstool-ng -cd crosstool-ng -git init -git fetch --depth=1 ${URL} ${REV} -git reset --hard FETCH_HEAD -./bootstrap -./configure --prefix=/usr/local -make -j$(nproc) -make install -cd .. -rm -rf crosstool-ng diff --git a/src/ci/docker/scripts/crosstool-ng.sh b/src/ci/docker/scripts/crosstool-ng.sh index a28d7bde2accf..c3ee19b8d2c1f 100644 --- a/src/ci/docker/scripts/crosstool-ng.sh +++ b/src/ci/docker/scripts/crosstool-ng.sh @@ -1,7 +1,7 @@ #!/bin/sh set -ex -CT_NG=1.25.0 +CT_NG=1.26.0 url="https://github.com/crosstool-ng/crosstool-ng/archive/crosstool-ng-$CT_NG.tar.gz" curl -Lf $url | tar xzf - From 02b01a46de62d26a2769b7546481a6ab08da82b8 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 21 Sep 2023 11:52:06 +0300 Subject: [PATCH 03/19] make region struct and add neccesasry types --- compiler/stable_mir/src/ty.rs | 49 +++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index 82007e3068340..b7cd57f753aa2 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -1,7 +1,7 @@ use super::{ mir::Safety, mir::{Body, Mutability}, - with, AllocId, DefId, + with, AllocId, DefId, Symbol, }; use crate::Opaque; use std::fmt::{self, Debug, Formatter}; @@ -34,7 +34,52 @@ pub struct Const { } type Ident = Opaque; -pub type Region = Opaque; +pub(crate) struct Region { + kind: RegionKind, +} + +enum RegionKind { + ReEarlyBound(EarlyBoundRegion), + ReLateBound(DebruijnIndex, BoundRegion), + ReFree(FreeRegion), + ReStatic, + ReVar(RegionVid), + RePlaceholder(Placeholder), + ReErased, + ReError(ErrorGuaranteed), +} + +pub(crate) type DebruijnIndex = u32; + +pub struct EarlyBoundRegion { + pub def_id: DefId, + pub index: u32, + pub name: Symbol, +} + +pub(crate) type BoundVar = u32; + +pub struct BoundRegion { + pub var: BoundVar, + pub kind: BoundRegionKind, +} + +pub struct FreeRegion { + pub scope: DefId, + pub bound_region: BoundRegionKind, +} + +pub(crate) type RegionVid = u32; + +pub(crate) type UniverseIndex = u32; + +pub struct Placeholder { + pub universe: UniverseIndex, + pub bound: T, +} + +pub(crate) type ErrorGuaranteed = (); + #[derive(Clone, Copy, PartialEq, Eq)] pub struct Span(pub usize); From e49aa04000b3443a8cf1650ee0a75d48ddf115d0 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 21 Sep 2023 12:01:30 +0300 Subject: [PATCH 04/19] add RegionDef --- compiler/rustc_smir/src/rustc_internal/mod.rs | 4 +++ compiler/rustc_smir/src/rustc_smir/mod.rs | 28 ++++++++++++++++++- compiler/stable_mir/src/ty.rs | 9 ++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 441aafd1257ba..1a9dea99f643f 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -86,6 +86,10 @@ impl<'tcx> Tables<'tcx> { stable_mir::ty::ImplDef(self.create_def_id(did)) } + pub fn region_def(&mut self, did: DefId) -> stable_mir::ty::RegionDef { + stable_mir::ty::RegionDef(self.create_def_id(did)) + } + pub fn prov(&mut self, aid: AllocId) -> stable_mir::ty::Prov { stable_mir::ty::Prov(self.create_alloc_id(aid)) } diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 5ff17613b4e56..890faa4538e23 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -7,7 +7,12 @@ //! //! For now, we are developing everything inside `rustc`, thus, we keep this module private. -use hir::def::DefKind; +use crate::rustc_internal::{self, opaque}; +use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx}; +use crate::stable_mir::ty::{ + EarlyBoundRegion, FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy, +}; +use crate::stable_mir::{self, CompilerError, Context}; use rustc_hir as hir; use rustc_middle::mir; use rustc_middle::mir::interpret::{alloc_range, AllocId}; @@ -1506,6 +1511,27 @@ impl<'tcx> Stable<'tcx> for ty::Region<'tcx> { } } +impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> { + type T = stable_mir::ty::RegionKind; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + match self { + ty::ReEarlyBound(early_reg) => RegionKind::ReEarlyBound(EarlyBoundRegion { + def_id: tables.region_def(early_reg.def_id), + index: early_reg.index, + name: early_reg.name.to_string(), + }), + ty::ReLateBound(_, _) => todo!(), + ty::ReFree(_) => todo!(), + ty::ReStatic => todo!(), + ty::ReVar(_) => todo!(), + ty::RePlaceholder(_) => todo!(), + ty::ReErased => todo!(), + ty::ReError(_) => todo!(), + } + } +} + impl<'tcx> Stable<'tcx> for rustc_span::Span { type T = stable_mir::ty::Span; diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index b7cd57f753aa2..f6f433c2bad01 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -38,7 +38,7 @@ pub(crate) struct Region { kind: RegionKind, } -enum RegionKind { +pub enum RegionKind { ReEarlyBound(EarlyBoundRegion), ReLateBound(DebruijnIndex, BoundRegion), ReFree(FreeRegion), @@ -52,7 +52,7 @@ enum RegionKind { pub(crate) type DebruijnIndex = u32; pub struct EarlyBoundRegion { - pub def_id: DefId, + pub def_id: RegionDef, pub index: u32, pub name: Symbol, } @@ -65,7 +65,7 @@ pub struct BoundRegion { } pub struct FreeRegion { - pub scope: DefId, + pub scope: RegionDef, pub bound_region: BoundRegionKind, } @@ -197,6 +197,9 @@ pub struct ConstDef(pub DefId); #[derive(Clone, PartialEq, Eq, Debug)] pub struct ImplDef(pub DefId); +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct RegionDef(pub(crate) DefId); + #[derive(Clone, Debug)] pub struct GenericArgs(pub Vec); From 5dc2214884483496ef8cf4a8fa7f1be189669974 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 21 Sep 2023 12:12:06 +0300 Subject: [PATCH 05/19] add stable for RegionKind --- compiler/rustc_smir/src/rustc_smir/mod.rs | 37 ++++++++++++++++------- compiler/stable_mir/src/ty.rs | 2 +- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 890faa4538e23..73a4798527d63 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -10,7 +10,8 @@ use crate::rustc_internal::{self, opaque}; use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx}; use crate::stable_mir::ty::{ - EarlyBoundRegion, FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy, + BoundRegion, EarlyBoundRegion, FloatTy, FreeRegion, GenericParamDef, IntTy, Movability, Region, + RigidTy, Span, TyKind, UintTy, }; use crate::stable_mir::{self, CompilerError, Context}; use rustc_hir as hir; @@ -1505,9 +1506,8 @@ impl<'tcx> Stable<'tcx> for ty::ImplPolarity { impl<'tcx> Stable<'tcx> for ty::Region<'tcx> { type T = stable_mir::ty::Region; - fn stable(&self, _: &mut Tables<'tcx>) -> Self::T { - // FIXME: add a real implementation of stable regions - opaque(self) + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + Region { kind: self.kind().stable(tables) } } } @@ -1515,19 +1515,34 @@ impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> { type T = stable_mir::ty::RegionKind; fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + use crate::stable_mir::ty::RegionKind; match self { ty::ReEarlyBound(early_reg) => RegionKind::ReEarlyBound(EarlyBoundRegion { def_id: tables.region_def(early_reg.def_id), index: early_reg.index, name: early_reg.name.to_string(), }), - ty::ReLateBound(_, _) => todo!(), - ty::ReFree(_) => todo!(), - ty::ReStatic => todo!(), - ty::ReVar(_) => todo!(), - ty::RePlaceholder(_) => todo!(), - ty::ReErased => todo!(), - ty::ReError(_) => todo!(), + ty::ReLateBound(db_index, bound_reg) => RegionKind::ReLateBound( + db_index.as_u32(), + BoundRegion { var: bound_reg.var.as_u32(), kind: bound_reg.kind.stable(tables) }, + ), + ty::ReFree(free_reg) => RegionKind::ReFree(FreeRegion { + scope: tables.region_def(free_reg.scope), + bound_region: free_reg.bound_region.stable(tables), + }), + ty::ReStatic => RegionKind::ReStatic, + ty::ReVar(vid_reg) => RegionKind::ReVar(vid_reg.as_u32()), + ty::RePlaceholder(place_holder) => { + RegionKind::RePlaceholder(stable_mir::ty::Placeholder { + universe: place_holder.universe.as_u32(), + bound: BoundRegion { + var: place_holder.bound.var.as_u32(), + kind: place_holder.bound.kind.stable(tables), + }, + }) + } + ty::ReErased => RegionKind::ReErased, + ty::ReError(_) => RegionKind::ReError(()), } } } diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index f6f433c2bad01..a47621337f0f1 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -35,7 +35,7 @@ pub struct Const { type Ident = Opaque; pub(crate) struct Region { - kind: RegionKind, + pub kind: RegionKind, } pub enum RegionKind { From d83559939cf89f717d1fcd8266c5f457df6e37dd Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 21 Sep 2023 12:18:10 +0300 Subject: [PATCH 06/19] make reg public and add visit, fold --- compiler/stable_mir/src/fold.rs | 8 +++++++- compiler/stable_mir/src/ty.rs | 9 ++++++++- compiler/stable_mir/src/visitor.rs | 8 +++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/compiler/stable_mir/src/fold.rs b/compiler/stable_mir/src/fold.rs index 16ae62311aaf0..edaf55f2aeb81 100644 --- a/compiler/stable_mir/src/fold.rs +++ b/compiler/stable_mir/src/fold.rs @@ -4,7 +4,7 @@ use crate::Opaque; use super::ty::{ Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind, - GenericArgs, Promoted, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst, + GenericArgs, Promoted, Region, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst, }; pub trait Folder: Sized { @@ -106,6 +106,12 @@ impl Foldable for GenericArgs { } } +impl Foldable for Region { + fn super_fold(&self, _folder: &mut V) -> ControlFlow { + ControlFlow::Continue(self.clone()) + } +} + impl Foldable for GenericArgKind { fn super_fold(&self, folder: &mut V) -> ControlFlow { let mut this = self.clone(); diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index a47621337f0f1..d05fa4a2ed4bc 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -34,10 +34,13 @@ pub struct Const { } type Ident = Opaque; -pub(crate) struct Region { + +#[derive(Debug, Clone)] +pub struct Region { pub kind: RegionKind, } +#[derive(Debug, Clone)] pub enum RegionKind { ReEarlyBound(EarlyBoundRegion), ReLateBound(DebruijnIndex, BoundRegion), @@ -51,6 +54,7 @@ pub enum RegionKind { pub(crate) type DebruijnIndex = u32; +#[derive(Debug, Clone)] pub struct EarlyBoundRegion { pub def_id: RegionDef, pub index: u32, @@ -59,11 +63,13 @@ pub struct EarlyBoundRegion { pub(crate) type BoundVar = u32; +#[derive(Debug, Clone)] pub struct BoundRegion { pub var: BoundVar, pub kind: BoundRegionKind, } +#[derive(Debug, Clone)] pub struct FreeRegion { pub scope: RegionDef, pub bound_region: BoundRegionKind, @@ -73,6 +79,7 @@ pub(crate) type RegionVid = u32; pub(crate) type UniverseIndex = u32; +#[derive(Debug, Clone)] pub struct Placeholder { pub universe: UniverseIndex, pub bound: T, diff --git a/compiler/stable_mir/src/visitor.rs b/compiler/stable_mir/src/visitor.rs index 9c3b4cd994a48..8d5a25008d78a 100644 --- a/compiler/stable_mir/src/visitor.rs +++ b/compiler/stable_mir/src/visitor.rs @@ -4,7 +4,7 @@ use crate::Opaque; use super::ty::{ Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs, - Promoted, RigidTy, TermKind, Ty, UnevaluatedConst, + Promoted, Region, RigidTy, TermKind, Ty, UnevaluatedConst, }; pub trait Visitor: Sized { @@ -101,6 +101,12 @@ impl Visitable for GenericArgs { } } +impl Visitable for Region { + fn super_visit(&self, _visitor: &mut V) -> ControlFlow { + ControlFlow::Continue(()) + } +} + impl Visitable for GenericArgKind { fn super_visit(&self, visitor: &mut V) -> ControlFlow { match self { From 2069e8c218fcc8428ae400fe91b245c977859036 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 11:36:53 +0300 Subject: [PATCH 07/19] fix imports --- compiler/rustc_smir/src/rustc_smir/mod.rs | 11 +++-------- compiler/stable_mir/src/ty.rs | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 73a4798527d63..2623ce8fd11f6 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -7,13 +7,8 @@ //! //! For now, we are developing everything inside `rustc`, thus, we keep this module private. -use crate::rustc_internal::{self, opaque}; -use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx}; -use crate::stable_mir::ty::{ - BoundRegion, EarlyBoundRegion, FloatTy, FreeRegion, GenericParamDef, IntTy, Movability, Region, - RigidTy, Span, TyKind, UintTy, -}; -use crate::stable_mir::{self, CompilerError, Context}; +use crate::rustc_smir::hir::def::DefKind; +use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region}; use rustc_hir as hir; use rustc_middle::mir; use rustc_middle::mir::interpret::{alloc_range, AllocId}; @@ -1515,7 +1510,7 @@ impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> { type T = stable_mir::ty::RegionKind; fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { - use crate::stable_mir::ty::RegionKind; + use stable_mir::ty::RegionKind; match self { ty::ReEarlyBound(early_reg) => RegionKind::ReEarlyBound(EarlyBoundRegion { def_id: tables.region_def(early_reg.def_id), diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index d05fa4a2ed4bc..20ef514e4ab03 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -205,7 +205,7 @@ pub struct ConstDef(pub DefId); pub struct ImplDef(pub DefId); #[derive(Clone, PartialEq, Eq, Debug)] -pub struct RegionDef(pub(crate) DefId); +pub struct RegionDef(pub DefId); #[derive(Clone, Debug)] pub struct GenericArgs(pub Vec); From da2f897e590be03eb4acddfd9df804545b738b65 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 11:43:21 +0300 Subject: [PATCH 08/19] remove un-needed variants --- compiler/rustc_smir/src/rustc_smir/mod.rs | 9 ++------- compiler/stable_mir/src/ty.rs | 13 ------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 2623ce8fd11f6..3b6bacaa168ff 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -8,7 +8,7 @@ //! For now, we are developing everything inside `rustc`, thus, we keep this module private. use crate::rustc_smir::hir::def::DefKind; -use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region}; +use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region}; use rustc_hir as hir; use rustc_middle::mir; use rustc_middle::mir::interpret::{alloc_range, AllocId}; @@ -1521,12 +1521,7 @@ impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> { db_index.as_u32(), BoundRegion { var: bound_reg.var.as_u32(), kind: bound_reg.kind.stable(tables) }, ), - ty::ReFree(free_reg) => RegionKind::ReFree(FreeRegion { - scope: tables.region_def(free_reg.scope), - bound_region: free_reg.bound_region.stable(tables), - }), ty::ReStatic => RegionKind::ReStatic, - ty::ReVar(vid_reg) => RegionKind::ReVar(vid_reg.as_u32()), ty::RePlaceholder(place_holder) => { RegionKind::RePlaceholder(stable_mir::ty::Placeholder { universe: place_holder.universe.as_u32(), @@ -1537,7 +1532,7 @@ impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> { }) } ty::ReErased => RegionKind::ReErased, - ty::ReError(_) => RegionKind::ReError(()), + _=> unimplemented!() } } } diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index 20ef514e4ab03..6029e3c11adef 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -44,12 +44,9 @@ pub struct Region { pub enum RegionKind { ReEarlyBound(EarlyBoundRegion), ReLateBound(DebruijnIndex, BoundRegion), - ReFree(FreeRegion), ReStatic, - ReVar(RegionVid), RePlaceholder(Placeholder), ReErased, - ReError(ErrorGuaranteed), } pub(crate) type DebruijnIndex = u32; @@ -69,14 +66,6 @@ pub struct BoundRegion { pub kind: BoundRegionKind, } -#[derive(Debug, Clone)] -pub struct FreeRegion { - pub scope: RegionDef, - pub bound_region: BoundRegionKind, -} - -pub(crate) type RegionVid = u32; - pub(crate) type UniverseIndex = u32; #[derive(Debug, Clone)] @@ -85,8 +74,6 @@ pub struct Placeholder { pub bound: T, } -pub(crate) type ErrorGuaranteed = (); - #[derive(Clone, Copy, PartialEq, Eq)] pub struct Span(pub usize); From bb17fe8bf5b32cd7fefbab95b6b1b7ee04628dbd Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 12:32:15 +0300 Subject: [PATCH 09/19] add real folder to Region --- compiler/rustc_smir/src/rustc_smir/mod.rs | 2 +- compiler/stable_mir/src/fold.rs | 41 +++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 3b6bacaa168ff..128ba07654458 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -1532,7 +1532,7 @@ impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> { }) } ty::ReErased => RegionKind::ReErased, - _=> unimplemented!() + _ => unimplemented!(), } } } diff --git a/compiler/stable_mir/src/fold.rs b/compiler/stable_mir/src/fold.rs index edaf55f2aeb81..bdc80bc8aa37c 100644 --- a/compiler/stable_mir/src/fold.rs +++ b/compiler/stable_mir/src/fold.rs @@ -1,6 +1,9 @@ use std::ops::ControlFlow; -use crate::Opaque; +use crate::{ + ty::{self, BoundRegion, BoundRegionKind}, + Opaque, +}; use super::ty::{ Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind, @@ -15,6 +18,9 @@ pub trait Folder: Sized { fn fold_const(&mut self, c: &Const) -> ControlFlow { c.super_fold(self) } + fn visit_reg(&mut self, reg: &Region) -> ControlFlow { + reg.super_fold(self) + } } pub trait Foldable: Sized + Clone { @@ -107,8 +113,39 @@ impl Foldable for GenericArgs { } impl Foldable for Region { + fn fold(&self, folder: &mut V) -> ControlFlow { + folder.visit_reg(self) + } + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let mut kind = self.kind.clone(); + match &mut kind { + crate::ty::RegionKind::ReEarlyBound(_) => {} + crate::ty::RegionKind::ReLateBound(_, bound_reg) => { + *bound_reg = bound_reg.fold(folder)? + } + crate::ty::RegionKind::ReStatic => {} + crate::ty::RegionKind::RePlaceholder(bound_reg) => { + bound_reg.bound = bound_reg.bound.fold(folder)? + } + crate::ty::RegionKind::ReErased => {} + } + ControlFlow::Continue(ty::Region { kind: kind }.into()) + } +} + +impl Foldable for BoundRegion { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + ControlFlow::Continue(BoundRegion { var: self.var, kind: self.kind.fold(folder)? }) + } +} + +impl Foldable for BoundRegionKind { fn super_fold(&self, _folder: &mut V) -> ControlFlow { - ControlFlow::Continue(self.clone()) + match self { + BoundRegionKind::BrAnon => ControlFlow::Continue(self.clone()), + BoundRegionKind::BrNamed(_, _) => ControlFlow::Continue(self.clone()), + BoundRegionKind::BrEnv => ControlFlow::Continue(self.clone()), + } } } From fed72e06644db562c043f2f9cec99f2b2d9cba9d Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 12:47:21 +0300 Subject: [PATCH 10/19] add visitor for Region --- compiler/stable_mir/src/visitor.rs | 37 ++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/compiler/stable_mir/src/visitor.rs b/compiler/stable_mir/src/visitor.rs index 8d5a25008d78a..a26a1d8cfb9a4 100644 --- a/compiler/stable_mir/src/visitor.rs +++ b/compiler/stable_mir/src/visitor.rs @@ -1,6 +1,9 @@ use std::ops::ControlFlow; -use crate::Opaque; +use crate::{ + ty::{BoundRegion, BoundRegionKind}, + Opaque, +}; use super::ty::{ Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs, @@ -15,6 +18,9 @@ pub trait Visitor: Sized { fn visit_const(&mut self, c: &Const) -> ControlFlow { c.super_visit(self) } + fn visit_reg(&mut self, reg: &Region) -> ControlFlow { + reg.super_visit(self) + } } pub trait Visitable { @@ -102,11 +108,38 @@ impl Visitable for GenericArgs { } impl Visitable for Region { - fn super_visit(&self, _visitor: &mut V) -> ControlFlow { + fn visit(&self, visitor: &mut V) -> ControlFlow { + visitor.visit_reg(self) + } + + fn super_visit(&self, visitor: &mut V) -> ControlFlow { + match self.kind.clone() { + crate::ty::RegionKind::ReEarlyBound(_) => {} + crate::ty::RegionKind::ReLateBound(_, bound_reg) => bound_reg.visit(visitor)?, + crate::ty::RegionKind::ReStatic => {} + crate::ty::RegionKind::RePlaceholder(bound_reg) => bound_reg.bound.visit(visitor)?, + crate::ty::RegionKind::ReErased => {} + } ControlFlow::Continue(()) } } +impl Visitable for BoundRegion { + fn super_visit(&self, visitor: &mut V) -> ControlFlow { + self.kind.visit(visitor) + } +} + +impl Visitable for BoundRegionKind { + fn super_visit(&self, _visitor: &mut V) -> ControlFlow { + match self { + BoundRegionKind::BrAnon => ControlFlow::Continue(()), + BoundRegionKind::BrNamed(_, _) => ControlFlow::Continue(()), + BoundRegionKind::BrEnv => ControlFlow::Continue(()), + } + } +} + impl Visitable for GenericArgKind { fn super_visit(&self, visitor: &mut V) -> ControlFlow { match self { From 9f2e15d2328df0d32c5515c7b9a922b810403982 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 19:21:12 +0300 Subject: [PATCH 11/19] change visit to fold for ty and reg --- compiler/stable_mir/src/fold.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/stable_mir/src/fold.rs b/compiler/stable_mir/src/fold.rs index bdc80bc8aa37c..90853c104bb5b 100644 --- a/compiler/stable_mir/src/fold.rs +++ b/compiler/stable_mir/src/fold.rs @@ -12,13 +12,13 @@ use super::ty::{ pub trait Folder: Sized { type Break; - fn visit_ty(&mut self, ty: &Ty) -> ControlFlow { + fn fold_ty(&mut self, ty: &Ty) -> ControlFlow { ty.super_fold(self) } fn fold_const(&mut self, c: &Const) -> ControlFlow { c.super_fold(self) } - fn visit_reg(&mut self, reg: &Region) -> ControlFlow { + fn fold_reg(&mut self, reg: &Region) -> ControlFlow { reg.super_fold(self) } } @@ -32,7 +32,7 @@ pub trait Foldable: Sized + Clone { impl Foldable for Ty { fn fold(&self, folder: &mut V) -> ControlFlow { - folder.visit_ty(self) + folder.fold_ty(self) } fn super_fold(&self, folder: &mut V) -> ControlFlow { let mut kind = self.kind(); @@ -114,7 +114,7 @@ impl Foldable for GenericArgs { impl Foldable for Region { fn fold(&self, folder: &mut V) -> ControlFlow { - folder.visit_reg(self) + folder.fold_reg(self) } fn super_fold(&self, folder: &mut V) -> ControlFlow { let mut kind = self.kind.clone(); @@ -257,7 +257,7 @@ pub enum Never {} impl Folder for GenericArgs { type Break = Never; - fn visit_ty(&mut self, ty: &Ty) -> ControlFlow { + fn fold_ty(&mut self, ty: &Ty) -> ControlFlow { ControlFlow::Continue(match ty.kind() { TyKind::Param(p) => self[p], _ => *ty, From 8c41cd0d78caed65ebd2f057d5209fc564f9cc10 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 19:43:28 +0300 Subject: [PATCH 12/19] simplify fold --- compiler/stable_mir/src/fold.rs | 37 +++------------------------------ 1 file changed, 3 insertions(+), 34 deletions(-) diff --git a/compiler/stable_mir/src/fold.rs b/compiler/stable_mir/src/fold.rs index 90853c104bb5b..65b743811ce52 100644 --- a/compiler/stable_mir/src/fold.rs +++ b/compiler/stable_mir/src/fold.rs @@ -1,9 +1,6 @@ use std::ops::ControlFlow; -use crate::{ - ty::{self, BoundRegion, BoundRegionKind}, - Opaque, -}; +use crate::Opaque; use super::ty::{ Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind, @@ -116,36 +113,8 @@ impl Foldable for Region { fn fold(&self, folder: &mut V) -> ControlFlow { folder.fold_reg(self) } - fn super_fold(&self, folder: &mut V) -> ControlFlow { - let mut kind = self.kind.clone(); - match &mut kind { - crate::ty::RegionKind::ReEarlyBound(_) => {} - crate::ty::RegionKind::ReLateBound(_, bound_reg) => { - *bound_reg = bound_reg.fold(folder)? - } - crate::ty::RegionKind::ReStatic => {} - crate::ty::RegionKind::RePlaceholder(bound_reg) => { - bound_reg.bound = bound_reg.bound.fold(folder)? - } - crate::ty::RegionKind::ReErased => {} - } - ControlFlow::Continue(ty::Region { kind: kind }.into()) - } -} - -impl Foldable for BoundRegion { - fn super_fold(&self, folder: &mut V) -> ControlFlow { - ControlFlow::Continue(BoundRegion { var: self.var, kind: self.kind.fold(folder)? }) - } -} - -impl Foldable for BoundRegionKind { - fn super_fold(&self, _folder: &mut V) -> ControlFlow { - match self { - BoundRegionKind::BrAnon => ControlFlow::Continue(self.clone()), - BoundRegionKind::BrNamed(_, _) => ControlFlow::Continue(self.clone()), - BoundRegionKind::BrEnv => ControlFlow::Continue(self.clone()), - } + fn super_fold(&self, _: &mut V) -> ControlFlow { + ControlFlow::Continue(self.clone()) } } From 0cca109473161c5c71de53d8ea0c9b7ff0a9b5c4 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 19:46:39 +0300 Subject: [PATCH 13/19] visit and fold ty::ref --- compiler/stable_mir/src/fold.rs | 5 ++++- compiler/stable_mir/src/visitor.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/stable_mir/src/fold.rs b/compiler/stable_mir/src/fold.rs index 65b743811ce52..4e0b4bc7a73a5 100644 --- a/compiler/stable_mir/src/fold.rs +++ b/compiler/stable_mir/src/fold.rs @@ -148,7 +148,10 @@ impl Foldable for RigidTy { } RigidTy::Slice(inner) => *inner = inner.fold(folder)?, RigidTy::RawPtr(ty, _) => *ty = ty.fold(folder)?, - RigidTy::Ref(_, ty, _) => *ty = ty.fold(folder)?, + RigidTy::Ref(reg, ty, _) => { + *reg = reg.fold(folder)?; + *ty = ty.fold(folder)? + } RigidTy::FnDef(_, args) => *args = args.fold(folder)?, RigidTy::FnPtr(sig) => *sig = sig.fold(folder)?, RigidTy::Closure(_, args) => *args = args.fold(folder)?, diff --git a/compiler/stable_mir/src/visitor.rs b/compiler/stable_mir/src/visitor.rs index a26a1d8cfb9a4..848a3114b6b56 100644 --- a/compiler/stable_mir/src/visitor.rs +++ b/compiler/stable_mir/src/visitor.rs @@ -167,7 +167,10 @@ impl Visitable for RigidTy { } RigidTy::Slice(inner) => inner.visit(visitor), RigidTy::RawPtr(ty, _) => ty.visit(visitor), - RigidTy::Ref(_, ty, _) => ty.visit(visitor), + RigidTy::Ref(reg, ty, _) => { + reg.visit(visitor); + ty.visit(visitor) + } RigidTy::FnDef(_, args) => args.visit(visitor), RigidTy::FnPtr(sig) => sig.visit(visitor), RigidTy::Closure(_, args) => args.visit(visitor), From eb779038de59e028ebbd6533072acec7b5fc98af Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 19:51:49 +0300 Subject: [PATCH 14/19] simplify visit --- compiler/stable_mir/src/visitor.rs | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/compiler/stable_mir/src/visitor.rs b/compiler/stable_mir/src/visitor.rs index 848a3114b6b56..961009581388d 100644 --- a/compiler/stable_mir/src/visitor.rs +++ b/compiler/stable_mir/src/visitor.rs @@ -1,9 +1,6 @@ use std::ops::ControlFlow; -use crate::{ - ty::{BoundRegion, BoundRegionKind}, - Opaque, -}; +use crate::Opaque; use super::ty::{ Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs, @@ -112,34 +109,11 @@ impl Visitable for Region { visitor.visit_reg(self) } - fn super_visit(&self, visitor: &mut V) -> ControlFlow { - match self.kind.clone() { - crate::ty::RegionKind::ReEarlyBound(_) => {} - crate::ty::RegionKind::ReLateBound(_, bound_reg) => bound_reg.visit(visitor)?, - crate::ty::RegionKind::ReStatic => {} - crate::ty::RegionKind::RePlaceholder(bound_reg) => bound_reg.bound.visit(visitor)?, - crate::ty::RegionKind::ReErased => {} - } + fn super_visit(&self, _: &mut V) -> ControlFlow { ControlFlow::Continue(()) } } -impl Visitable for BoundRegion { - fn super_visit(&self, visitor: &mut V) -> ControlFlow { - self.kind.visit(visitor) - } -} - -impl Visitable for BoundRegionKind { - fn super_visit(&self, _visitor: &mut V) -> ControlFlow { - match self { - BoundRegionKind::BrAnon => ControlFlow::Continue(()), - BoundRegionKind::BrNamed(_, _) => ControlFlow::Continue(()), - BoundRegionKind::BrEnv => ControlFlow::Continue(()), - } - } -} - impl Visitable for GenericArgKind { fn super_visit(&self, visitor: &mut V) -> ControlFlow { match self { From 34f10e2ab98622e42c38c97530070aa69ad51d86 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Sep 2023 19:52:41 +0300 Subject: [PATCH 15/19] remove unimplemented --- compiler/rustc_smir/src/rustc_smir/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 128ba07654458..c6c97ce35e8b9 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -1532,7 +1532,7 @@ impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> { }) } ty::ReErased => RegionKind::ReErased, - _ => unimplemented!(), + _ => unreachable!("{self:?}"), } } } From a95f20c9adb45be65d579251b3f7e4401b8aa3b1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 28 Sep 2023 10:18:56 -0700 Subject: [PATCH 16/19] Add Exclusive forwarding impls (FnOnce, FnMut, Generator) --- library/core/src/sync/exclusive.rs | 46 +++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/library/core/src/sync/exclusive.rs b/library/core/src/sync/exclusive.rs index 3f3e19c55d4ba..ff538d55c6079 100644 --- a/library/core/src/sync/exclusive.rs +++ b/library/core/src/sync/exclusive.rs @@ -2,6 +2,8 @@ use core::fmt; use core::future::Future; +use core::marker::Tuple; +use core::ops::{Generator, GeneratorState}; use core::pin::Pin; use core::task::{Context, Poll}; @@ -168,10 +170,52 @@ impl From for Exclusive { } #[unstable(feature = "exclusive_wrapper", issue = "98407")] -impl Future for Exclusive { +impl FnOnce for Exclusive +where + F: FnOnce, + Args: Tuple, +{ + type Output = F::Output; + + extern "rust-call" fn call_once(self, args: Args) -> Self::Output { + self.into_inner().call_once(args) + } +} + +#[unstable(feature = "exclusive_wrapper", issue = "98407")] +impl FnMut for Exclusive +where + F: FnMut, + Args: Tuple, +{ + extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output { + self.get_mut().call_mut(args) + } +} + +#[unstable(feature = "exclusive_wrapper", issue = "98407")] +impl Future for Exclusive +where + T: Future + ?Sized, +{ type Output = T::Output; + #[inline] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { self.get_pin_mut().poll(cx) } } + +#[unstable(feature = "generator_trait", issue = "43122")] // also #98407 +impl Generator for Exclusive +where + G: Generator + ?Sized, +{ + type Yield = G::Yield; + type Return = G::Return; + + #[inline] + fn resume(self: Pin<&mut Self>, arg: R) -> GeneratorState { + G::resume(self.get_pin_mut(), arg) + } +} From b83dfb5c5a6e9c976200490c89ff00726c425ad2 Mon Sep 17 00:00:00 2001 From: bohan Date: Wed, 31 May 2023 10:10:16 +0800 Subject: [PATCH 17/19] fix(suggestion): insert projection to associated types --- compiler/rustc_hir_analysis/src/check/mod.rs | 67 +++++++++++-------- .../auxiliary/extern-issue-98562.rs | 26 +++++++ tests/ui/suggestions/issue-98562.rs | 12 ++++ tests/ui/suggestions/issue-98562.stderr | 11 +++ tests/ui/suggestions/missing-assoc-fn.stderr | 2 +- 5 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 tests/ui/suggestions/auxiliary/extern-issue-98562.rs create mode 100644 tests/ui/suggestions/issue-98562.rs create mode 100644 tests/ui/suggestions/issue-98562.stderr diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 88c98fa979e1c..5fa65f33c763c 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -329,41 +329,52 @@ fn bounds_from_generic_predicates<'tcx>( _ => {} } } - let generics = if types.is_empty() { - "".to_string() - } else { - format!( - "<{}>", - types - .keys() - .filter_map(|t| match t.kind() { - ty::Param(_) => Some(t.to_string()), - // Avoid suggesting the following: - // fn foo::Bar>(_: T) where T: Trait, ::Bar: Other {} - _ => None, - }) - .collect::>() - .join(", ") - ) - }; + let mut where_clauses = vec![]; + let mut types_str = vec![]; for (ty, bounds) in types { - where_clauses - .extend(bounds.into_iter().map(|bound| format!("{}: {}", ty, tcx.def_path_str(bound)))); - } - for projection in &projections { - let p = projection.skip_binder(); - // FIXME: this is not currently supported syntax, we should be looking at the `types` and - // insert the associated types where they correspond, but for now let's be "lazy" and - // propose this instead of the following valid resugaring: - // `T: Trait, Trait::Assoc = K` → `T: Trait` - where_clauses.push(format!("{} = {}", tcx.def_path_str(p.projection_ty.def_id), p.term)); + if let ty::Param(_) = ty.kind() { + let mut bounds_str = vec![]; + for bound in bounds { + let mut projections_str = vec![]; + for projection in &projections { + let p = projection.skip_binder(); + let alias_ty = p.projection_ty; + if bound == tcx.parent(alias_ty.def_id) && alias_ty.self_ty() == ty { + let name = tcx.item_name(alias_ty.def_id); + projections_str.push(format!("{} = {}", name, p.term)); + } + } + let bound_def_path = tcx.def_path_str(bound); + if projections_str.is_empty() { + where_clauses.push(format!("{}: {}", ty, bound_def_path)); + } else { + bounds_str.push(format!("{}<{}>", bound_def_path, projections_str.join(", "))); + } + } + if bounds_str.is_empty() { + types_str.push(ty.to_string()); + } else { + types_str.push(format!("{}: {}", ty, bounds_str.join(" + "))); + } + } else { + // Avoid suggesting the following: + // fn foo::Bar>(_: T) where T: Trait, ::Bar: Other {} + where_clauses.extend( + bounds.into_iter().map(|bound| format!("{}: {}", ty, tcx.def_path_str(bound))), + ); + } } + + let generics = + if types_str.is_empty() { "".to_string() } else { format!("<{}>", types_str.join(", ")) }; + let where_clauses = if where_clauses.is_empty() { - String::new() + "".to_string() } else { format!(" where {}", where_clauses.join(", ")) }; + (generics, where_clauses) } diff --git a/tests/ui/suggestions/auxiliary/extern-issue-98562.rs b/tests/ui/suggestions/auxiliary/extern-issue-98562.rs new file mode 100644 index 0000000000000..948e40549c6db --- /dev/null +++ b/tests/ui/suggestions/auxiliary/extern-issue-98562.rs @@ -0,0 +1,26 @@ +pub trait TraitE { + type I3; +} + +pub trait TraitD { + type I3; +} + +pub trait TraitC { + type I1; + type I2; +} + +pub trait TraitB { + type Item; +} + +pub trait TraitA { + fn baz< + U: TraitC + TraitD + TraitE, + V: TraitD + >(_: U, _: V) -> Self + where + U: TraitB, + ::Item: Copy; +} diff --git a/tests/ui/suggestions/issue-98562.rs b/tests/ui/suggestions/issue-98562.rs new file mode 100644 index 0000000000000..de04050d59312 --- /dev/null +++ b/tests/ui/suggestions/issue-98562.rs @@ -0,0 +1,12 @@ +// aux-build:extern-issue-98562.rs + +extern crate extern_issue_98562; +use extern_issue_98562::TraitA; + +struct X; +impl TraitA for X { + //~^ ERROR not all trait items implemented +} +//~^ HELP implement the missing item: `fn baz + TraitD, V: TraitD>(_: U, _: V) -> Self where U: TraitE, U: TraitB, ::Item: Copy { todo!() }` + +fn main() {} diff --git a/tests/ui/suggestions/issue-98562.stderr b/tests/ui/suggestions/issue-98562.stderr new file mode 100644 index 0000000000000..7897fa441a24f --- /dev/null +++ b/tests/ui/suggestions/issue-98562.stderr @@ -0,0 +1,11 @@ +error[E0046]: not all trait items implemented, missing: `baz` + --> $DIR/issue-98562.rs:7:1 + | +LL | impl TraitA for X { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `baz` in implementation + | + = help: implement the missing item: `fn baz + TraitD, V: TraitD>(_: U, _: V) -> Self where U: TraitE, U: TraitB, ::Item: Copy { todo!() }` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0046`. diff --git a/tests/ui/suggestions/missing-assoc-fn.stderr b/tests/ui/suggestions/missing-assoc-fn.stderr index 77fa956287816..84cb6e98553ee 100644 --- a/tests/ui/suggestions/missing-assoc-fn.stderr +++ b/tests/ui/suggestions/missing-assoc-fn.stderr @@ -28,7 +28,7 @@ error[E0046]: not all trait items implemented, missing: `from_iter` LL | impl FromIterator<()> for X { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `from_iter` in implementation | - = help: implement the missing item: `fn from_iter(_: T) -> Self where T: IntoIterator, std::iter::IntoIterator::Item = () { todo!() }` + = help: implement the missing item: `fn from_iter>(_: T) -> Self { todo!() }` error: aborting due to 3 previous errors From 90f317b2de742bcc986cd40c6736ffc3813d036c Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Fri, 29 Sep 2023 12:24:35 +0200 Subject: [PATCH 18/19] add needs-relocation-model-pic to compiletest --- src/tools/compiletest/src/common.rs | 6 ++++++ src/tools/compiletest/src/header/needs.rs | 5 +++++ tests/ui/abi/relocation_model_pic.rs | 3 +-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index ba273489eb8af..0e1bf0c6c2dce 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -580,6 +580,8 @@ pub struct TargetCfg { pub(crate) sanitizers: Vec, #[serde(rename = "supports-xray", default)] pub(crate) xray: bool, + #[serde(default = "default_reloc_model")] + pub(crate) relocation_model: String, } impl TargetCfg { @@ -592,6 +594,10 @@ fn default_os() -> String { "none".into() } +fn default_reloc_model() -> String { + "pic".into() +} + #[derive(Eq, PartialEq, Clone, Debug, Default, serde::Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Endian { diff --git a/src/tools/compiletest/src/header/needs.rs b/src/tools/compiletest/src/header/needs.rs index 1113721fff612..2b7a4387cebc1 100644 --- a/src/tools/compiletest/src/header/needs.rs +++ b/src/tools/compiletest/src/header/needs.rs @@ -134,6 +134,11 @@ pub(super) fn handle_needs( condition: config.target_cfg().dynamic_linking, ignore_reason: "ignored on targets without dynamic linking", }, + Need { + name: "needs-relocation-model-pic", + condition: config.target_cfg().relocation_model == "pic", + ignore_reason: "ignored on targets without PIC relocation model", + }, ]; let (name, comment) = match ln.split_once([':', ' ']) { diff --git a/tests/ui/abi/relocation_model_pic.rs b/tests/ui/abi/relocation_model_pic.rs index 0cfc44cd09d80..cca2e8db74d75 100644 --- a/tests/ui/abi/relocation_model_pic.rs +++ b/tests/ui/abi/relocation_model_pic.rs @@ -1,7 +1,6 @@ // run-pass // compile-flags: -C relocation-model=pic -// ignore-emscripten no pic -// ignore-wasm +// needs-relocation-model-pic #![feature(cfg_relocation_model)] From 3853774df8d8217fb9788be3c0636b9a06da740f Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 14 Jun 2023 12:19:21 +0200 Subject: [PATCH 19/19] mark relevant tests as requiring unwinding --- tests/incremental/change_crate_dep_kind.rs | 1 + tests/ui/panics/short-ice-remove-middle-frames-2.rs | 1 + tests/ui/panics/short-ice-remove-middle-frames-2.run.stderr | 2 +- tests/ui/panics/short-ice-remove-middle-frames.rs | 1 + tests/ui/panics/short-ice-remove-middle-frames.run.stderr | 2 +- 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/incremental/change_crate_dep_kind.rs b/tests/incremental/change_crate_dep_kind.rs index f518266016e09..b9f74340472c0 100644 --- a/tests/incremental/change_crate_dep_kind.rs +++ b/tests/incremental/change_crate_dep_kind.rs @@ -5,6 +5,7 @@ // needs-unwind // revisions:cfail1 cfail2 // compile-flags: -Z query-dep-graph -Cpanic=unwind +// needs-unwind // build-pass (FIXME(62277): could be check-pass?) #![feature(panic_unwind)] diff --git a/tests/ui/panics/short-ice-remove-middle-frames-2.rs b/tests/ui/panics/short-ice-remove-middle-frames-2.rs index 38a80f8b6703a..751959f55bb6e 100644 --- a/tests/ui/panics/short-ice-remove-middle-frames-2.rs +++ b/tests/ui/panics/short-ice-remove-middle-frames-2.rs @@ -2,6 +2,7 @@ // run-fail // check-run-results // exec-env:RUST_BACKTRACE=1 +// needs-unwind // ignore-android FIXME #17520 // ignore-wasm no panic support // ignore-openbsd no support for libbacktrace without filename diff --git a/tests/ui/panics/short-ice-remove-middle-frames-2.run.stderr b/tests/ui/panics/short-ice-remove-middle-frames-2.run.stderr index 2b648a0cad2ea..664ebaa4c51a6 100644 --- a/tests/ui/panics/short-ice-remove-middle-frames-2.run.stderr +++ b/tests/ui/panics/short-ice-remove-middle-frames-2.run.stderr @@ -1,4 +1,4 @@ -thread 'main' panicked at $DIR/short-ice-remove-middle-frames-2.rs:56:5: +thread 'main' panicked at $DIR/short-ice-remove-middle-frames-2.rs:57:5: debug!!! stack backtrace: 0: std::panicking::begin_panic diff --git a/tests/ui/panics/short-ice-remove-middle-frames.rs b/tests/ui/panics/short-ice-remove-middle-frames.rs index c872084f0333e..134e13233da51 100644 --- a/tests/ui/panics/short-ice-remove-middle-frames.rs +++ b/tests/ui/panics/short-ice-remove-middle-frames.rs @@ -2,6 +2,7 @@ // run-fail // check-run-results // exec-env:RUST_BACKTRACE=1 +// needs-unwind // ignore-android FIXME #17520 // ignore-wasm no panic support // ignore-openbsd no support for libbacktrace without filename diff --git a/tests/ui/panics/short-ice-remove-middle-frames.run.stderr b/tests/ui/panics/short-ice-remove-middle-frames.run.stderr index 5b37268409679..bc252fde1f6ab 100644 --- a/tests/ui/panics/short-ice-remove-middle-frames.run.stderr +++ b/tests/ui/panics/short-ice-remove-middle-frames.run.stderr @@ -1,4 +1,4 @@ -thread 'main' panicked at $DIR/short-ice-remove-middle-frames.rs:52:5: +thread 'main' panicked at $DIR/short-ice-remove-middle-frames.rs:53:5: debug!!! stack backtrace: 0: std::panicking::begin_panic