Skip to content
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

rustc: use a separate copy of P for HIR than for AST. #61995

Merged
merged 3 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/cfg/construct.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::cfg::*;
use crate::middle::region;
use rustc_data_structures::graph::implementation as graph;
use syntax::ptr::P;
use crate::ty::{self, TyCtxt};

use crate::hir::{self, PatKind};
use crate::hir::def_id::DefId;
use crate::hir::ptr::P;

struct CFGBuilder<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
Expand Down
24 changes: 13 additions & 11 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::hir::map::{DefKey, DefPathData, Definitions};
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
use crate::hir::def::{Res, DefKind, PartialRes, PerNS};
use crate::hir::{GenericArg, ConstArg};
use crate::hir::ptr::P;
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
ELIDED_LIFETIMES_IN_PATHS};
use crate::middle::cstore::CrateStore;
Expand All @@ -61,7 +62,6 @@ use syntax::ast::*;
use syntax::errors;
use syntax::ext::hygiene::{Mark, SyntaxContext};
use syntax::print::pprust;
use syntax::ptr::P;
use syntax::source_map::{self, respan, ExpnInfo, CompilerDesugaringKind, Spanned};
use syntax::source_map::CompilerDesugaringKind::IfTemporary;
use syntax::std_inject;
Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl<'a> LoweringContext<'a> {
},
);

lowered_generics.params = lowered_generics
let mut lowered_params: Vec<_> = lowered_generics
.params
.into_iter()
.chain(in_band_defs)
Expand All @@ -1121,14 +1121,16 @@ impl<'a> LoweringContext<'a> {
// unsorted generic parameters at the moment, so we make sure
// that they're ordered correctly here for now. (When we chain
// the `in_band_defs`, we might make the order unsorted.)
lowered_generics.params.sort_by_key(|param| {
lowered_params.sort_by_key(|param| {
match param.kind {
hir::GenericParamKind::Lifetime { .. } => ParamKindOrd::Lifetime,
hir::GenericParamKind::Type { .. } => ParamKindOrd::Type,
hir::GenericParamKind::Const { .. } => ParamKindOrd::Const,
}
});

lowered_generics.params = lowered_params.into();

(lowered_generics, res)
}

Expand All @@ -1155,13 +1157,13 @@ impl<'a> LoweringContext<'a> {
&mut self,
capture_clause: CaptureBy,
closure_node_id: NodeId,
ret_ty: Option<&Ty>,
ret_ty: Option<syntax::ptr::P<Ty>>,
span: Span,
body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
) -> hir::ExprKind {
let capture_clause = self.lower_capture_clause(capture_clause);
let output = match ret_ty {
Some(ty) => FunctionRetTy::Ty(P(ty.clone())),
Some(ty) => FunctionRetTy::Ty(ty),
None => FunctionRetTy::Default(span),
};
let ast_decl = FnDecl {
Expand Down Expand Up @@ -2725,7 +2727,7 @@ impl<'a> LoweringContext<'a> {

// ::std::future::Future<future_params>
let future_path =
self.std_path(span, &[sym::future, sym::Future], Some(future_params), false);
P(self.std_path(span, &[sym::future, sym::Future], Some(future_params), false));

hir::GenericBound::Trait(
hir::PolyTraitRef {
Expand Down Expand Up @@ -3094,7 +3096,7 @@ impl<'a> LoweringContext<'a> {

fn lower_trait_ref(&mut self, p: &TraitRef, itctx: ImplTraitContext<'_>) -> hir::TraitRef {
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
hir::QPath::Resolved(None, path) => path.and_then(|path| path),
hir::QPath::Resolved(None, path) => path,
qpath => bug!("lower_trait_ref: unexpected QPath `{:?}`", qpath),
};
hir::TraitRef {
Expand Down Expand Up @@ -3620,7 +3622,7 @@ impl<'a> LoweringContext<'a> {
hir::Item {
hir_id: new_id,
ident,
attrs: attrs.clone(),
attrs: attrs.into_iter().cloned().collect(),
node: item,
vis,
span,
Expand Down Expand Up @@ -3705,7 +3707,7 @@ impl<'a> LoweringContext<'a> {
hir::Item {
hir_id: new_hir_id,
ident,
attrs: attrs.clone(),
attrs: attrs.into_iter().cloned().collect(),
node: item,
vis,
span: use_tree.span,
Expand Down Expand Up @@ -4567,7 +4569,7 @@ impl<'a> LoweringContext<'a> {
// `|x: u8| future_from_generator(|| -> X { ... })`.
let body_id = this.lower_fn_body(&outer_decl, |this| {
let async_ret_ty = if let FunctionRetTy::Ty(ty) = &decl.output {
Some(&**ty)
Some(ty.clone())
} else { None };
let async_body = this.make_async_expr(
capture_clause, closure_id, async_ret_ty, body.span,
Expand Down Expand Up @@ -5577,7 +5579,7 @@ impl<'a> LoweringContext<'a> {
let principal = hir::PolyTraitRef {
bound_generic_params: hir::HirVec::new(),
trait_ref: hir::TraitRef {
path: path.and_then(|path| path),
path,
hir_ref_id: hir_id,
},
span,
Expand Down
13 changes: 8 additions & 5 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use self::UnsafeSource::*;

use crate::hir::def::{Res, DefKind};
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
use crate::hir::ptr::P;
use crate::util::nodemap::{NodeMap, FxHashSet};
use crate::mir::mono::Linkage;

Expand All @@ -23,7 +24,6 @@ use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, AsmDialect};
use syntax::ast::{Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
use syntax::attr::{InlineAttr, OptimizeAttr};
use syntax::ext::hygiene::SyntaxContext;
use syntax::ptr::P;
use syntax::symbol::{Symbol, kw};
use syntax::tokenstream::TokenStream;
use syntax::util::parser::ExprPrecedence;
Expand Down Expand Up @@ -63,6 +63,7 @@ pub mod lowering;
pub mod map;
pub mod pat_util;
pub mod print;
pub mod ptr;
pub mod upvars;

/// Uniquely identifies a node in the HIR of the current crate. It is
Expand Down Expand Up @@ -1979,13 +1980,15 @@ pub struct InlineAsmOutput {
pub span: Span,
}

// NOTE(eddyb) This is used within MIR as well, so unlike the rest of the HIR,
// it needs to be `Clone` and use plain `Vec<T>` instead of `HirVec<T>`.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
pub struct InlineAsm {
pub asm: Symbol,
pub asm_str_style: StrStyle,
pub outputs: HirVec<InlineAsmOutput>,
pub inputs: HirVec<Symbol>,
pub clobbers: HirVec<Symbol>,
pub outputs: Vec<InlineAsmOutput>,
pub inputs: Vec<Symbol>,
pub clobbers: Vec<Symbol>,
eddyb marked this conversation as resolved.
Show resolved Hide resolved
pub volatile: bool,
pub alignstack: bool,
pub dialect: AsmDialect,
Expand Down Expand Up @@ -2217,7 +2220,7 @@ pub enum UseKind {
/// within the resolution map.
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable)]
pub struct TraitRef {
pub path: Path,
pub path: P<Path>,
// Don't hash the ref_id. It is tracked via the thing it is used to access
#[stable_hasher(ignore)]
pub hir_ref_id: HirId,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use syntax::parse::lexer::comments;
use syntax::print::pp::{self, Breaks};
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
use syntax::print::pprust::{self, PrintState};
use syntax::ptr::P;
use syntax::symbol::kw;
use syntax::util::parser::{self, AssocOp, Fixity};
use syntax_pos::{self, BytePos, FileName};

use crate::hir;
use crate::hir::{PatKind, GenericBound, TraitBoundModifier, RangeEnd};
use crate::hir::{GenericParam, GenericParamKind, GenericArg};
use crate::hir::ptr::P;

use std::borrow::Cow;
use std::cell::Cell;
Expand Down
141 changes: 141 additions & 0 deletions src/librustc/hir/ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// HACK(eddyb) this is a copy of `syntax::ptr`, minus the mutation (the HIR is
// frozen anyway). The only reason for doing this instead of replacing `P<T>`
// with `Box<T>` in HIR, is that `&Box<[T]>` doesn't implement `IntoIterator`.

use std::fmt::{self, Display, Debug};
use std::iter::FromIterator;
use std::ops::Deref;
use std::{slice, vec};

use serialize::{Encodable, Decodable, Encoder, Decoder};

use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
HashStable};
/// An owned smart pointer.
#[derive(Hash, PartialEq, Eq)]
pub struct P<T: ?Sized> {
ptr: Box<T>
}

/// Construct a `P<T>` from a `T` value.
#[allow(non_snake_case)]
pub fn P<T: 'static>(value: T) -> P<T> {
P {
ptr: box value
}
}

impl<T: 'static> P<T> {
// HACK(eddyb) used by HIR lowering in a few places still.
// NOTE: do not make this more public than `pub(super)`.
pub(super) fn into_inner(self) -> T {
*self.ptr
}
}

impl<T: ?Sized> Deref for P<T> {
type Target = T;

fn deref(&self) -> &T {
&self.ptr
}
}

impl<T: ?Sized + Debug> Debug for P<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.ptr, f)
}
}

impl<T: Display> Display for P<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&**self, f)
}
}

impl<T: 'static + Decodable> Decodable for P<T> {
fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> {
Decodable::decode(d).map(P)
}
}

impl<T: Encodable> Encodable for P<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
(**self).encode(s)
}
}

impl<T> P<[T]> {
pub const fn new() -> P<[T]> {
// HACK(eddyb) bypass the lack of a `const fn` to create an empty `Box<[T]>`
// (as trait methods, `default` in this case, can't be `const fn` yet).
P {
ptr: unsafe {
use std::ptr::NonNull;
std::mem::transmute(NonNull::<[T; 0]>::dangling() as NonNull<[T]>)
},
}
}

#[inline(never)]
pub fn from_vec(v: Vec<T>) -> P<[T]> {
P { ptr: v.into_boxed_slice() }
}

// HACK(eddyb) used by HIR lowering in a few places still.
// NOTE: do not make this more public than `pub(super)`,
// and do not make this into an `IntoIterator` impl.
pub(super) fn into_iter(self) -> vec::IntoIter<T> {
self.ptr.into_vec().into_iter()
}
}


impl<T> Default for P<[T]> {
/// Creates an empty `P<[T]>`.
fn default() -> P<[T]> {
P::new()
}
}

impl<T> From<Vec<T>> for P<[T]> {
fn from(v: Vec<T>) -> Self {
P::from_vec(v)
}
}

impl<T> FromIterator<T> for P<[T]> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> {
P::from_vec(iter.into_iter().collect())
}
}

impl<'a, T> IntoIterator for &'a P<[T]> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.ptr.into_iter()
}
}

impl<T: Encodable> Encodable for P<[T]> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
Encodable::encode(&**self, s)
}
}

impl<T: Decodable> Decodable for P<[T]> {
fn decode<D: Decoder>(d: &mut D) -> Result<P<[T]>, D::Error> {
Ok(P::from_vec(Decodable::decode(d)?))
}
}

impl<CTX, T> HashStable<CTX> for P<T>
where T: ?Sized + HashStable<CTX>
{
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut CTX,
hasher: &mut StableHasher<W>) {
(**self).hash_stable(hcx, hasher);
}
}
2 changes: 2 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#![feature(arbitrary_self_types)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(const_transmute)]
#![feature(core_intrinsics)]
#![feature(drain_filter)]
#![feature(inner_deref)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use self::OverloadedCallType::*;

use crate::hir::def::{CtorOf, Res, DefKind};
use crate::hir::def_id::DefId;
use crate::hir::ptr::P;
use crate::infer::InferCtxt;
use crate::middle::mem_categorization as mc;
use crate::middle::region;
use crate::ty::{self, DefIdTree, TyCtxt, adjustment};

use crate::hir::{self, PatKind};
use std::rc::Rc;
use syntax::ptr::P;
use syntax_pos::Span;
use crate::util::nodemap::ItemLocalSet;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ use self::VarKind::*;

use crate::hir::def::*;
use crate::hir::Node;
use crate::hir::ptr::P;
use crate::ty::{self, TyCtxt};
use crate::ty::query::Providers;
use crate::lint;
Expand All @@ -111,7 +112,6 @@ use std::io::prelude::*;
use std::io;
use std::rc::Rc;
use syntax::ast;
use syntax::ptr::P;
use syntax::symbol::{kw, sym};
use syntax_pos::Span;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use crate::hir::def::{Res, DefKind};
use crate::hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use crate::hir::map::Map;
use crate::hir::ptr::P;
use crate::hir::{GenericArg, GenericParam, ItemLocalId, LifetimeName, Node, ParamName};
use crate::ty::{self, DefIdTree, GenericParamDefKind, TyCtxt};

Expand All @@ -21,7 +22,6 @@ use std::cell::Cell;
use std::mem::replace;
use syntax::ast;
use syntax::attr;
use syntax::ptr::P;
use syntax::symbol::{kw, sym};
use syntax_pos::Span;

Expand Down
Loading