Skip to content

Rollup of 5 pull requests #139417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 5, 2025
Merged
5 changes: 2 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4432,7 +4432,7 @@ dependencies = [
"rustc_span",
"rustc_target",
"scoped-tls",
"stable_mir",
"serde",
"tracing",
]

Expand Down Expand Up @@ -4990,8 +4990,7 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
name = "stable_mir"
version = "0.1.0-preview"
dependencies = [
"scoped-tls",
"serde",
"rustc_smir",
]

[[package]]
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,6 @@ where
.filter(|c| matches!(c.source, CandidateSource::ParamEnv(_)))
.map(|c| c.result)
.collect();

return if let Some(response) = self.try_merge_responses(&where_bounds) {
Ok((response, Some(TraitGoalProvenVia::ParamEnv)))
} else {
Expand All @@ -1322,9 +1321,18 @@ where
};
}

// If there are *only* global where bounds, then make sure to return that this
// is still reported as being proven-via the param-env so that rigid projections
// operate correctly.
let proven_via =
if candidates.iter().all(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
TraitGoalProvenVia::ParamEnv
} else {
TraitGoalProvenVia::Misc
};
let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
if let Some(response) = self.try_merge_responses(&all_candidates) {
Ok((response, Some(TraitGoalProvenVia::Misc)))
Ok((response, Some(proven_via)))
} else {
self.flounder(&all_candidates).map(|r| (r, None))
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
scoped-tls = "1.0"
stable_mir = {path = "../stable_mir" }
serde = { version = "1.0.125", features = [ "derive" ] }
tracing = "0.1"
# tidy-alphabetical-end
2 changes: 2 additions & 0 deletions compiler/rustc_smir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ pub mod rustc_internal;

// Make this module private for now since external users should not call these directly.
mod rustc_smir;

pub mod stable_mir;
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_internal/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use stable_mir::{CrateItem, CrateNum, DefId};

use super::RustcInternal;
use crate::rustc_smir::Tables;
use crate::stable_mir;

impl RustcInternal for CrateItem {
type T<'tcx> = rustc_span::def_id::DefId;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use stable_mir::ty::IndexedVal;

use crate::rustc_smir::context::TablesWrapper;
use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

mod internal;
pub mod pretty;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_internal/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io;
use rustc_middle::ty::TyCtxt;

use super::run;
use crate::stable_mir;

pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io::Result<()> {
writeln!(
Expand Down
39 changes: 13 additions & 26 deletions compiler/rustc_smir/src/rustc_smir/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use stable_mir::mir::Mutability;
use stable_mir::ty::{Allocation, ProvenanceMap};

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

/// Creates new empty `Allocation` from given `Align`.
fn new_empty_allocation(align: Align) -> Allocation {
Expand All @@ -27,7 +28,7 @@ pub(crate) fn new_allocation<'tcx>(
tables: &mut Tables<'tcx>,
) -> Allocation {
try_new_allocation(ty, const_value, tables)
.expect(&format!("Failed to convert: {const_value:?} to {ty:?}"))
.unwrap_or_else(|_| panic!("Failed to convert: {const_value:?} to {ty:?}"))
}

#[allow(rustc::usage_of_qualified_ty)]
Expand All @@ -36,39 +37,30 @@ pub(crate) fn try_new_allocation<'tcx>(
const_value: ConstValue<'tcx>,
tables: &mut Tables<'tcx>,
) -> Result<Allocation, Error> {
let layout = tables
.tcx
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
.map_err(|e| e.stable(tables))?;
Ok(match const_value {
ConstValue::Scalar(scalar) => {
let size = scalar.size();
let align = tables
.tcx
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
.map_err(|e| e.stable(tables))?
.align;
let mut allocation =
rustc_middle::mir::interpret::Allocation::new(size, align.abi, AllocInit::Uninit);
let mut allocation = rustc_middle::mir::interpret::Allocation::new(
size,
layout.align.abi,
AllocInit::Uninit,
);
allocation
.write_scalar(&tables.tcx, alloc_range(Size::ZERO, size), scalar)
.map_err(|e| e.stable(tables))?;
allocation.stable(tables)
}
ConstValue::ZeroSized => {
let align = tables
.tcx
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
.map_err(|e| e.stable(tables))?
.align;
new_empty_allocation(align.abi)
}
ConstValue::ZeroSized => new_empty_allocation(layout.align.abi),
ConstValue::Slice { data, meta } => {
let alloc_id = tables.tcx.reserve_and_set_memory_alloc(data);
let ptr = Pointer::new(alloc_id.into(), Size::ZERO);
let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx);
let scalar_meta =
rustc_middle::mir::interpret::Scalar::from_target_usize(meta, &tables.tcx);
let layout = tables
.tcx
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
.map_err(|e| e.stable(tables))?;
let mut allocation = rustc_middle::mir::interpret::Allocation::new(
layout.size,
layout.align.abi,
Expand All @@ -92,12 +84,7 @@ pub(crate) fn try_new_allocation<'tcx>(
}
ConstValue::Indirect { alloc_id, offset } => {
let alloc = tables.tcx.global_alloc(alloc_id).unwrap_memory();
let ty_size = tables
.tcx
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
.map_err(|e| e.stable(tables))?
.size;
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)
allocation_filter(&alloc.0, alloc_range(offset, layout.size), tables)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_middle::mir::visit::MutVisitor;
use rustc_middle::ty::{self, TyCtxt};

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

/// Builds a monomorphic body for a given instance.
pub(crate) struct BodyBuilder<'tcx> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, I
use crate::rustc_internal::RustcInternal;
use crate::rustc_smir::builder::BodyBuilder;
use crate::rustc_smir::{Stable, Tables, alloc, filter_def_ids, new_item_kind, smir_crate};
use crate::stable_mir;

impl<'tcx> Context for TablesWrapper<'tcx> {
fn target_info(&self) -> MachineInfo {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use stable_mir::target::MachineSize as Size;
use stable_mir::ty::{Align, IndexedVal, VariantIdx};

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for rustc_abi::VariantIdx {
type T = VariantIdx;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_middle::mir::interpret::AllocError;
use rustc_middle::ty::layout::LayoutError;

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for LayoutError<'tcx> {
type T = stable_mir::Error;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use stable_mir::ty::{Allocation, ConstantKind, MirConst};
use stable_mir::{Error, opaque};

use crate::rustc_smir::{Stable, Tables, alloc};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
type T = stable_mir::mir::Body;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use rustc_abi::FieldIdx;

use crate::rustc_smir::{Stable, Tables};
use crate::stable_mir;

mod abi;
mod error;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use stable_mir::ty::{
};

use crate::rustc_smir::{Stable, Tables, alloc};
use crate::stable_mir;

impl<'tcx> Stable<'tcx> for ty::AliasTyKind {
type T = stable_mir::ty::AliasKind;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use stable_mir::{CtorKind, ItemKind};
use tracing::debug;

use crate::rustc_internal::IndexMap;
use crate::stable_mir;

mod alloc;
mod builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use std::num::NonZero;
use std::ops::RangeInclusive;

use serde::Serialize;
use stable_mir::compiler_interface::with;
use stable_mir::mir::FieldIdx;
use stable_mir::target::{MachineInfo, MachineSize as Size};
use stable_mir::ty::{Align, IndexedVal, Ty, VariantIdx};
use stable_mir::{Error, Opaque, error};

use crate::compiler_interface::with;
use crate::mir::FieldIdx;
use crate::target::{MachineInfo, MachineSize as Size};
use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
use crate::{Error, Opaque, error};
use crate::stable_mir;

/// A function ABI definition.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
Expand Down Expand Up @@ -149,7 +150,7 @@ pub enum FieldsShape {
Arbitrary {
/// Offsets for the first byte of each field,
/// ordered to match the source definition order.
/// I.e.: It follows the same order as [crate::ty::VariantDef::fields()].
/// I.e.: It follows the same order as [super::ty::VariantDef::fields()].
/// This vector does not go in increasing order.
offsets: Vec<Size>,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@

use std::cell::Cell;

use crate::abi::{FnAbi, Layout, LayoutShape};
use crate::crate_def::Attribute;
use crate::mir::alloc::{AllocId, GlobalAlloc};
use crate::mir::mono::{Instance, InstanceDef, StaticDef};
use crate::mir::{BinOp, Body, Place, UnOp};
use crate::target::MachineInfo;
use crate::ty::{
use stable_mir::abi::{FnAbi, Layout, LayoutShape};
use stable_mir::crate_def::Attribute;
use stable_mir::mir::alloc::{AllocId, GlobalAlloc};
use stable_mir::mir::mono::{Instance, InstanceDef, StaticDef};
use stable_mir::mir::{BinOp, Body, Place, UnOp};
use stable_mir::target::MachineInfo;
use stable_mir::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
};
use crate::{
use stable_mir::{
AssocItems, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls,
ItemKind, Symbol, TraitDecls, mir,
};

use crate::stable_mir;

/// This trait defines the interface between stable_mir and the Rust compiler.
/// Do not use this directly.
pub trait Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//! such as, a function, a trait, an enum, and any other definitions.

use serde::Serialize;
use stable_mir::ty::{GenericArgs, Span, Ty};
use stable_mir::{AssocItems, Crate, Symbol, with};

use crate::ty::{GenericArgs, Span, Ty};
use crate::{AssocItems, Crate, Symbol, with};
use crate::stable_mir;

/// A unique identification number for each item accessible for the current compilation unit.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
use std::io::Read;

use serde::Serialize;
use stable_mir::mir::mono::{Instance, StaticDef};
use stable_mir::target::{Endian, MachineInfo};
use stable_mir::ty::{Allocation, Binder, ExistentialTraitRef, IndexedVal, Ty};
use stable_mir::{Error, with};

use crate::mir::mono::{Instance, StaticDef};
use crate::target::{Endian, MachineInfo};
use crate::ty::{Allocation, Binder, ExistentialTraitRef, IndexedVal, Ty};
use crate::{Error, with};
use crate::stable_mir;

/// An allocation in the SMIR global memory can be either a function pointer,
/// a static, or a "real" allocation with some data in it.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::io;

use serde::Serialize;

use crate::compiler_interface::with;
use crate::mir::pretty::function_body;
use crate::ty::{
use stable_mir::compiler_interface::with;
use stable_mir::mir::pretty::function_body;
use stable_mir::ty::{
AdtDef, ClosureDef, CoroutineClosureDef, CoroutineDef, GenericArgs, MirConst, Movability,
Region, RigidTy, Ty, TyConst, TyKind, VariantIdx,
};
use crate::{Error, Opaque, Span, Symbol};
use stable_mir::{Error, Opaque, Span, Symbol};

use crate::stable_mir;

/// The SMIR representation of a single function.
#[derive(Clone, Debug, Serialize)]
Expand Down Expand Up @@ -565,7 +566,7 @@ pub enum Rvalue {
///
/// **Needs clarification**: Are there weird additional semantics here related to the runtime
/// nature of this operation?
ThreadLocalRef(crate::CrateItem),
ThreadLocalRef(stable_mir::CrateItem),

/// Computes a value as described by the operation.
NullaryOp(NullOp, Ty),
Expand Down
Loading
Loading