Skip to content

Commit

Permalink
Auto merge of rust-lang#97873 - Dylan-DPC:rollup-g6ptsdq, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Rollup of 5 pull requests

Successful merges:

 - rust-lang#97276 (Stabilize `const_intrinsic_copy`)
 - rust-lang#97763 (Allow ptr_from_addr_cast to fail)
 - rust-lang#97846 (Specify DWARF alignment in bits, not bytes.)
 - rust-lang#97848 (Impl Traits lowering minor refactors)
 - rust-lang#97865 (remove `BorrowckMode`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jun 8, 2022
2 parents 1a97162 + 5d1a366 commit 09d52bc
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 109 deletions.
7 changes: 4 additions & 3 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::Span;
use rustc_target::spec::abi;
use smallvec::{smallvec, SmallVec};
use tracing::debug;

use std::iter;

Expand Down Expand Up @@ -117,6 +116,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
self.owners[def_id]
}

#[instrument(level = "debug", skip(self, c))]
fn lower_crate(&mut self, c: &Crate) {
debug_assert_eq!(self.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);

Expand All @@ -127,6 +127,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
})
}

#[instrument(level = "debug", skip(self))]
fn lower_item(&mut self, item: &Item) {
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
}
Expand Down Expand Up @@ -485,6 +486,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
(ty, self.lower_const_body(span, body))
}

#[instrument(level = "debug", skip(self))]
fn lower_use_tree(
&mut self,
tree: &UseTree,
Expand All @@ -494,8 +496,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
ident: &mut Ident,
attrs: Option<&'hir [Attribute]>,
) -> hir::ItemKind<'hir> {
debug!("lower_use_tree(tree={:?})", tree);

let path = &tree.prefix;
let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect();

Expand Down Expand Up @@ -1298,6 +1298,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

/// Return the pair of the lowered `generics` as `hir::Generics` and the evaluation of `f` with
/// the carried impl trait definitions and bounds.
#[instrument(level = "debug", skip(self, f))]
fn lower_generics<T>(
&mut self,
generics: &Generics,
Expand Down
125 changes: 79 additions & 46 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)]

#[macro_use]
extern crate tracing;

use rustc_ast::visit;
use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust;
Expand All @@ -63,7 +66,6 @@ use rustc_span::{Span, DUMMY_SP};

use smallvec::SmallVec;
use std::collections::hash_map::Entry;
use tracing::{debug, trace};

macro_rules! arena_vec {
($this:expr; $($x:expr),*) => (
Expand Down Expand Up @@ -439,7 +441,7 @@ pub fn lower_crate<'a, 'hir>(
arena.alloc(krate)
}

#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Debug)]
enum ParamMode {
/// Any path in a type context.
Explicit,
Expand All @@ -455,6 +457,7 @@ enum ParenthesizedGenericArgs {
}

impl<'a, 'hir> LoweringContext<'a, 'hir> {
#[instrument(level = "debug", skip(self, f))]
fn with_hir_id_owner(
&mut self,
owner: NodeId,
Expand Down Expand Up @@ -599,12 +602,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.lower_node_id(node_id)
}

#[instrument(level = "trace", skip(self))]
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
let res: Result<Res, ()> = res.apply_id(|id| {
let owner = self.current_hir_id_owner;
let local_id = self.node_id_to_local_id.get(&id).copied().ok_or(())?;
Ok(hir::HirId { owner, local_id })
});
trace!(?res);

// We may fail to find a HirId when the Res points to a Local from an enclosing HIR owner.
// This can happen when trying to lower the return type `x` in erroneous code like
// async fn foo(x: u8) -> x {}
Expand Down Expand Up @@ -851,6 +857,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// ```
///
/// returns a `hir::TypeBinding` representing `Item`.
#[instrument(level = "debug", skip(self))]
fn lower_assoc_ty_constraint(
&mut self,
constraint: &AssocConstraint,
Expand Down Expand Up @@ -1011,6 +1018,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
err.emit();
}

#[instrument(level = "debug", skip(self))]
fn lower_generic_arg(
&mut self,
arg: &ast::GenericArg,
Expand Down Expand Up @@ -1081,6 +1089,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}

#[instrument(level = "debug", skip(self))]
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext) -> &'hir hir::Ty<'hir> {
self.arena.alloc(self.lower_ty_direct(t, itctx))
}
Expand Down Expand Up @@ -1212,41 +1221,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
)
}
ImplTraitContext::Universal => {
// Add a definition for the in-band `Param`.
let def_id = self.resolver.local_def_id(def_node_id);

let hir_bounds =
self.lower_param_bounds(bounds, ImplTraitContext::Universal);
// Set the name to `impl Bound1 + Bound2`.
let span = t.span;
let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
let param = hir::GenericParam {
hir_id: self.lower_node_id(def_node_id),
name: ParamName::Plain(self.lower_ident(ident)),
pure_wrt_drop: false,
span: self.lower_span(span),
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
colon_span: None,
};
let (param, bounds, path) =
self.lower_generic_and_bounds(def_node_id, span, ident, bounds);
self.impl_trait_defs.push(param);

if let Some(preds) = self.lower_generic_bound_predicate(
ident,
def_node_id,
&GenericParamKind::Type { default: None },
hir_bounds,
hir::PredicateOrigin::ImplTrait,
) {
self.impl_trait_bounds.push(preds)
if let Some(bounds) = bounds {
self.impl_trait_bounds.push(bounds);
}

hir::TyKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
span: self.lower_span(span),
res: Res::Def(DefKind::TyParam, def_id.to_def_id()),
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
}),
))
path
}
ImplTraitContext::Disallowed(position) => {
let mut err = struct_span_err!(
Expand Down Expand Up @@ -1737,6 +1720,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
)
}

#[instrument(level = "trace", skip(self))]
fn lower_param_bound(
&mut self,
tpb: &GenericBound,
Expand Down Expand Up @@ -1862,8 +1846,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.arena.alloc_from_iter(self.lower_generic_params_mut(params))
}

#[instrument(level = "trace", skip(self))]
fn lower_generic_param(&mut self, param: &GenericParam) -> hir::GenericParam<'hir> {
let (name, kind) = match param.kind {
let (name, kind) = self.lower_generic_param_kind(param);

let hir_id = self.lower_node_id(param.id);
self.lower_attrs(hir_id, &param.attrs);
hir::GenericParam {
hir_id,
name,
span: self.lower_span(param.span()),
pure_wrt_drop: self.sess.contains_name(&param.attrs, sym::may_dangle),
kind,
colon_span: param.colon_span.map(|s| self.lower_span(s)),
}
}

fn lower_generic_param_kind(
&mut self,
param: &GenericParam,
) -> (hir::ParamName, hir::GenericParamKind<'hir>) {
match param.kind {
GenericParamKind::Lifetime => {
// AST resolution emitted an error on those parameters, so we lower them using
// `ParamName::Error`.
Expand Down Expand Up @@ -1897,17 +1900,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::GenericParamKind::Const { ty, default },
)
}
};

let hir_id = self.lower_node_id(param.id);
self.lower_attrs(hir_id, &param.attrs);
hir::GenericParam {
hir_id,
name,
span: self.lower_span(param.span()),
pure_wrt_drop: self.sess.contains_name(&param.attrs, sym::may_dangle),
kind,
colon_span: param.colon_span.map(|s| self.lower_span(s)),
}
}

Expand Down Expand Up @@ -1954,6 +1946,47 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx))
}

fn lower_generic_and_bounds(
&mut self,
node_id: NodeId,
span: Span,
ident: Ident,
bounds: &[GenericBound],
) -> (hir::GenericParam<'hir>, Option<hir::WherePredicate<'hir>>, hir::TyKind<'hir>) {
// Add a definition for the in-band `Param`.
let def_id = self.resolver.local_def_id(node_id);

let hir_bounds = self.lower_param_bounds(bounds, ImplTraitContext::Universal);
// Set the name to `impl Bound1 + Bound2`.
let param = hir::GenericParam {
hir_id: self.lower_node_id(node_id),
name: ParamName::Plain(self.lower_ident(ident)),
pure_wrt_drop: false,
span: self.lower_span(span),
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
colon_span: None,
};

let preds = self.lower_generic_bound_predicate(
ident,
node_id,
&GenericParamKind::Type { default: None },
hir_bounds,
hir::PredicateOrigin::ImplTrait,
);

let ty = hir::TyKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
span: self.lower_span(span),
res: Res::Def(DefKind::TyParam, def_id.to_def_id()),
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
}),
));

(param, preds, ty)
}

/// Lowers a block directly to an expression, presuming that it
/// has no attributes and is not targeted by a `break`.
fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use smallvec::smallvec;
use tracing::debug;

impl<'a, 'hir> LoweringContext<'a, 'hir> {
#[instrument(level = "trace", skip(self))]
pub(crate) fn lower_qpath(
&mut self,
id: NodeId,
Expand All @@ -23,7 +24,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
param_mode: ParamMode,
itctx: ImplTraitContext,
) -> hir::QPath<'hir> {
debug!("lower_qpath(id: {:?}, qself: {:?}, p: {:?})", id, qself, p);
let qself_position = qself.as_ref().map(|q| q.position);
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx));

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ pub fn build_global_var_di_node<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId, glo
is_local_to_unit,
global,
None,
global_align.bytes() as u32,
global_align.bits() as u32,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let addr = addr.to_machine_usize(self)?;

// Then turn address into pointer.
let ptr = M::ptr_from_addr_cast(&self, addr);
let ptr = M::ptr_from_addr_cast(&self, addr)?;
Ok(Scalar::from_maybe_pointer(ptr, self).into())
}

Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_const_eval/src/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
fn ptr_from_addr_cast(
ecx: &InterpCx<'mir, 'tcx, Self>,
addr: u64,
) -> Pointer<Option<Self::PointerTag>>;
) -> InterpResult<'tcx, Pointer<Option<Self::PointerTag>>>;

// FIXME: Transmuting an integer to a pointer should just always return a `None`
// provenance, but that causes problems with function pointers in Miri.
/// Hook for returning a pointer from a transmute-like operation on an addr.
/// This is only needed to support Miri's (unsound) "allow-ptr-int-transmute" flag.
fn ptr_from_addr_transmute(
ecx: &InterpCx<'mir, 'tcx, Self>,
addr: u64,
Expand Down Expand Up @@ -519,8 +518,10 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
fn ptr_from_addr_cast(
_ecx: &InterpCx<$mir, $tcx, Self>,
addr: u64,
) -> Pointer<Option<AllocId>> {
Pointer::new(None, Size::from_bytes(addr))
) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
// Allow these casts, but make the pointer not dereferenceable.
// (I.e., they behave like transmutation.)
Ok(Pointer::new(None, Size::from_bytes(addr)))
}

#[inline(always)]
Expand Down
17 changes: 0 additions & 17 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,23 +547,6 @@ pub enum PrintRequest {
LinkArgs,
}

#[derive(Copy, Clone)]
pub enum BorrowckMode {
Mir,
Migrate,
}

impl BorrowckMode {
/// Returns whether we should run the MIR-based borrow check, but also fall back
/// on the AST borrow check if the MIR-based one errors.
pub fn migrate(self) -> bool {
match self {
BorrowckMode::Mir => false,
BorrowckMode::Migrate => true,
}
}
}

pub enum Input {
/// Load source code from a file.
File(PathBuf),
Expand Down
Loading

0 comments on commit 09d52bc

Please sign in to comment.