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: remove Ty::{is_self, has_self_ty}. #53677

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 2 additions & 11 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2286,15 +2286,6 @@ impl<'a> LoweringContext<'a> {
param
}
GenericParamKind::Type { ref default, .. } => {
// Don't expose `Self` (recovered "keyword used as ident" parse error).
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
// Instead, use gensym("Self") to create a distinct name that looks the same.
let ident = if param.ident.name == keywords::SelfType.name() {
param.ident.gensym()
} else {
param.ident
};

let add_bounds = add_bounds.get(&param.id).map_or(&[][..], |x| &x);
if !add_bounds.is_empty() {
let params = self.lower_param_bounds(add_bounds, itctx.reborrow()).into_iter();
Expand All @@ -2305,11 +2296,11 @@ impl<'a> LoweringContext<'a> {

hir::GenericParam {
id: self.lower_node_id(param.id).node_id,
name: hir::ParamName::Plain(ident),
name: hir::ParamName::Plain(param.ident),
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
attrs: self.lower_attrs(&param.attrs),
bounds,
span: ident.span,
span: param.ident.span,
kind: hir::GenericParamKind::Type {
default: default.as_ref().map(|x| {
self.lower_ty(x, ImplTraitContext::Disallowed)
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ for ty::FloatVid

impl_stable_hash_for!(struct ty::ParamTy {
idx,
def_id,
name
});

Expand Down
52 changes: 21 additions & 31 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,37 +1112,27 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
{
// Attempt to obtain the span of the parameter so we can
// suggest adding an explicit lifetime bound to it.
let type_param_span = match (self.in_progress_tables, bound_kind) {
(Some(ref table), GenericKind::Param(ref param)) => {
let table = table.borrow();
table.local_id_root.and_then(|did| {
let generics = self.tcx.generics_of(did);
// Account for the case where `did` corresponds to `Self`, which doesn't have
// the expected type argument.
if !param.is_self() {
let type_param = generics.type_param(param, self.tcx);
let hir = &self.tcx.hir;
hir.as_local_node_id(type_param.def_id).map(|id| {
// Get the `hir::Param` to verify whether it already has any bounds.
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
// instead we suggest `T: 'a + 'b` in that case.
let mut has_bounds = false;
if let hir_map::NodeGenericParam(ref param) = hir.get(id) {
has_bounds = !param.bounds.is_empty();
}
let sp = hir.span(id);
// `sp` only covers `T`, change it so that it covers
// `T:` when appropriate
let sp = if has_bounds {
sp.to(self.tcx
.sess
.source_map()
.next_point(self.tcx.sess.source_map().next_point(sp)))
} else {
sp
};
(sp, has_bounds)
})
let type_param_span = match bound_kind {
GenericKind::Param(ref param) => {
self.tcx.hir.as_local_node_id(param.def_id).and_then(|id| {
// Get the `hir::Param` to verify whether it already has any bounds.
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
// instead we suggest `T: 'a + 'b` in that case.
// Also, `Self` isn't in the HIR so we rule it out here.
if let hir_map::NodeGenericParam(ref hir_param) = self.tcx.hir.get(id) {
let has_bounds = !hir_param.bounds.is_empty();
let sp = self.tcx.hir.span(id);
// `sp` only covers `T`, change it so that it covers
// `T:` when appropriate
let sp = if has_bounds {
sp.to(self.tcx
.sess
.source_map()
.next_point(self.tcx.sess.source_map().next_point(sp)))
} else {
sp
};
Some((sp, has_bounds))
} else {
None
}
Expand Down
23 changes: 14 additions & 9 deletions src/librustc/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
trait_def_id: DefId,
supertraits_only: bool) -> bool
{
let trait_self_ty = self.mk_self_type(trait_def_id);
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id));
let predicates = if supertraits_only {
self.super_predicates_of(trait_def_id)
Expand All @@ -183,7 +184,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
match predicate {
ty::Predicate::Trait(ref data) => {
// In the case of a trait predicate, we can skip the "self" type.
data.skip_binder().input_types().skip(1).any(|t| t.has_self_ty())
data.skip_binder().input_types().skip(1).any(|t| {
t.walk().any(|ty| ty == trait_self_ty)
})
}
ty::Predicate::Projection(..) |
ty::Predicate::WellFormed(..) |
Expand All @@ -200,10 +203,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

fn trait_has_sized_self(self, trait_def_id: DefId) -> bool {
self.generics_require_sized_self(trait_def_id)
self.generics_require_sized_self(trait_def_id, trait_def_id)
}

fn generics_require_sized_self(self, def_id: DefId) -> bool {
fn generics_require_sized_self(self, trait_def_id: DefId, def_id: DefId) -> bool {
let trait_self_ty = self.mk_self_type(trait_def_id);
let sized_def_id = match self.lang_items().sized_trait() {
Some(def_id) => def_id,
None => { return false; /* No Sized trait, can't require it! */ }
Expand All @@ -216,7 +220,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
.any(|predicate| {
match predicate {
ty::Predicate::Trait(ref trait_pred) if trait_pred.def_id() == sized_def_id => {
trait_pred.skip_binder().self_ty().is_self()
trait_pred.skip_binder().self_ty() == trait_self_ty
}
ty::Predicate::Projection(..) |
ty::Predicate::Trait(..) |
Expand All @@ -241,7 +245,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
{
// Any method that has a `Self : Sized` requisite is otherwise
// exempt from the regulations.
if self.generics_require_sized_self(method.def_id) {
if self.generics_require_sized_self(trait_def_id, method.def_id) {
return None;
}

Expand All @@ -258,7 +262,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
-> bool
{
// Any method that has a `Self : Sized` requisite can't be called.
if self.generics_require_sized_self(method.def_id) {
if self.generics_require_sized_self(trait_def_id, method.def_id) {
return false;
}

Expand Down Expand Up @@ -286,7 +290,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

let sig = self.fn_sig(method.def_id);

let self_ty = self.mk_self_type();
let self_ty = self.mk_self_type(trait_def_id);
let self_arg_ty = sig.skip_binder().inputs()[0];
if let ExplicitSelf::Other = ExplicitSelf::determine(self_arg_ty, |ty| ty == self_ty) {
return Some(MethodViolationCode::NonStandardSelfType);
Expand Down Expand Up @@ -367,12 +371,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
// object type, and we cannot resolve `Self as SomeOtherTrait`
// without knowing what `Self` is.

let trait_self_ty = self.mk_self_type(trait_def_id);
let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None;
let mut error = false;
ty.maybe_walk(|ty| {
match ty.sty {
ty::Param(ref param_ty) => {
if param_ty.is_self() {
ty::Param(_) => {
if ty == trait_self_ty {
error = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<'cx, 'gcx, 'tcx> Elaborator<'cx, 'gcx, 'tcx> {
},

Component::Param(p) => {
let ty = tcx.mk_ty_param(p.idx, p.name);
let ty = p.to_ty(tcx);
Some(ty::Predicate::TypeOutlives(
ty::Binder::dummy(ty::OutlivesPredicate(ty, r_min))))
},
Expand Down
16 changes: 7 additions & 9 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ use syntax::attr;
use syntax::source_map::MultiSpan;
use syntax::edition::Edition;
use syntax::feature_gate;
use syntax::symbol::{Symbol, keywords, InternedString};
use syntax::symbol::Symbol;
use syntax_pos::Span;

use hir;
Expand Down Expand Up @@ -2556,22 +2556,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.mk_ty(Infer(it))
}

pub fn mk_ty_param(self,
index: u32,
name: InternedString) -> Ty<'tcx> {
self.mk_ty(Param(ParamTy { idx: index, name: name }))
pub fn mk_ty_param(self, def: &ty::GenericParamDef) -> Ty<'tcx> {
ParamTy::for_def(def).to_ty(self)
}

pub fn mk_self_type(self) -> Ty<'tcx> {
self.mk_ty_param(0, keywords::SelfType.name().as_interned_str())
pub fn mk_self_type(self, trait_def_id: DefId) -> Ty<'tcx> {
ParamTy::for_self(trait_def_id).to_ty(self)
}

pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
pub fn mk_param(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
match param.kind {
GenericParamDefKind::Lifetime => {
self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
}
GenericParamDefKind::Type {..} => self.mk_ty_param(param.index, param.name).into(),
GenericParamDefKind::Type {..} => self.mk_ty_param(param).into(),
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/librustc/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
ty::Infer(ty::FreshIntTy(_)) => "skolemized integral type".to_string(),
ty::Infer(ty::FreshFloatTy(_)) => "skolemized floating-point type".to_string(),
ty::Projection(_) => "associated type".to_string(),
ty::Param(ref p) => {
if p.is_self() {
"Self".to_string()
} else {
"type parameter".to_string()
}
}
ty::Param(_) => format!("`{}` parameter", self),
ty::Anon(..) => "anonymized type".to_string(),
ty::Error => "type error".to_string(),
}
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,9 @@ impl FlagComputation {
self.add_flags(TypeFlags::HAS_TY_ERR)
}

&ty::Param(ref p) => {
&ty::Param(_) => {
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
if p.is_self() {
self.add_flags(TypeFlags::HAS_SELF);
} else {
self.add_flags(TypeFlags::HAS_PARAMS);
}
self.add_flags(TypeFlags::HAS_PARAMS);
}

&ty::Generator(_, ref substs, _) => {
Expand Down
3 changes: 0 additions & 3 deletions src/librustc/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
fn has_param_types(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_PARAMS)
}
fn has_self_ty(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_SELF)
}
fn has_infer_types(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_TY_INFER)
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,6 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
if
!self.tcx.sess.opts.debugging_opts.print_type_sizes ||
layout.ty.has_param_types() ||
layout.ty.has_self_ty() ||
!self.param_env.caller_bounds.is_empty()
{
return;
Expand Down Expand Up @@ -1300,7 +1299,7 @@ impl<'a, 'tcx> SizeSkeleton<'tcx> {
let tail = tcx.struct_tail(pointee);
match tail.sty {
ty::Param(_) | ty::Projection(_) => {
debug_assert!(tail.has_param_types() || tail.has_self_ty());
debug_assert!(tail.has_param_types());
Ok(SizeSkeleton::Pointer {
non_zero,
tail: tcx.erase_regions(&tail)
Expand Down
30 changes: 13 additions & 17 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,57 +423,54 @@ pub struct CReaderCacheKey {
bitflags! {
pub struct TypeFlags: u32 {
const HAS_PARAMS = 1 << 0;
const HAS_SELF = 1 << 1;
const HAS_TY_INFER = 1 << 2;
const HAS_RE_INFER = 1 << 3;
const HAS_RE_SKOL = 1 << 4;
const HAS_TY_INFER = 1 << 1;
const HAS_RE_INFER = 1 << 2;
const HAS_RE_SKOL = 1 << 3;

/// Does this have any `ReEarlyBound` regions? Used to
/// determine whether substitition is required, since those
/// represent regions that are bound in a `ty::Generics` and
/// hence may be substituted.
const HAS_RE_EARLY_BOUND = 1 << 5;
const HAS_RE_EARLY_BOUND = 1 << 4;

/// Does this have any region that "appears free" in the type?
/// Basically anything but `ReLateBound` and `ReErased`.
const HAS_FREE_REGIONS = 1 << 6;
const HAS_FREE_REGIONS = 1 << 5;

/// Is an error type reachable?
const HAS_TY_ERR = 1 << 7;
const HAS_PROJECTION = 1 << 8;
const HAS_TY_ERR = 1 << 6;
const HAS_PROJECTION = 1 << 7;

// FIXME: Rename this to the actual property since it's used for generators too
const HAS_TY_CLOSURE = 1 << 9;
const HAS_TY_CLOSURE = 1 << 8;

// true if there are "names" of types and regions and so forth
// that are local to a particular fn
const HAS_FREE_LOCAL_NAMES = 1 << 10;
const HAS_FREE_LOCAL_NAMES = 1 << 9;

// Present if the type belongs in a local type context.
// Only set for Infer other than Fresh.
const KEEP_IN_LOCAL_TCX = 1 << 11;
const KEEP_IN_LOCAL_TCX = 1 << 10;

// Is there a projection that does not involve a bound region?
// Currently we can't normalize projections w/ bound regions.
const HAS_NORMALIZABLE_PROJECTION = 1 << 12;
const HAS_NORMALIZABLE_PROJECTION = 1 << 11;

// Set if this includes a "canonical" type or region var --
// ought to be true only for the results of canonicalization.
const HAS_CANONICAL_VARS = 1 << 13;
const HAS_CANONICAL_VARS = 1 << 12;

/// Does this have any `ReLateBound` regions? Used to check
/// if a global bound is safe to evaluate.
const HAS_RE_LATE_BOUND = 1 << 14;
const HAS_RE_LATE_BOUND = 1 << 13;

const NEEDS_SUBST = TypeFlags::HAS_PARAMS.bits |
TypeFlags::HAS_SELF.bits |
TypeFlags::HAS_RE_EARLY_BOUND.bits;

// Flags representing the nominal content of a type,
// computed by FlagsComputation. If you add a new nominal
// flag, it should be added here too.
const NOMINAL_FLAGS = TypeFlags::HAS_PARAMS.bits |
TypeFlags::HAS_SELF.bits |
TypeFlags::HAS_TY_INFER.bits |
TypeFlags::HAS_RE_INFER.bits |
TypeFlags::HAS_RE_SKOL.bits |
Expand Down Expand Up @@ -1632,7 +1629,6 @@ impl<'tcx> ParamEnv<'tcx> {
if value.has_skol()
|| value.needs_infer()
|| value.has_param_types()
|| value.has_self_ty()
{
ParamEnvAnd {
param_env: self,
Expand Down
Loading