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 type & lifetime parameter names from the typesystem. #53661

Closed
wants to merge 10 commits into from
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
57 changes: 37 additions & 20 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,38 @@ impl<'a> DefCollector<'a> {
fn visit_async_fn(
&mut self,
id: NodeId,
async_node_id: NodeId,
return_impl_trait_id: NodeId,
name: Name,
span: Span,
visit_fn: impl FnOnce(&mut DefCollector<'a>)
header: &FnHeader,
generics: &'a Generics,
decl: &'a FnDecl,
body: &'a Block,
) {
let (closure_id, return_impl_trait_id) = match header.asyncness {
IsAsync::Async {
closure_id,
return_impl_trait_id,
} => (closure_id, return_impl_trait_id),
_ => unreachable!(),
};

// For async functions, we need to create their inner defs inside of a
// closure to match their desugared representation.
let fn_def_data = DefPathData::ValueNs(name.as_interned_str());
let fn_def = self.create_def(id, fn_def_data, ITEM_LIKE_SPACE, span);
return self.with_parent(fn_def, |this| {
this.create_def(return_impl_trait_id, DefPathData::ImplTrait, REGULAR_SPACE, span);
let closure_def = this.create_def(async_node_id,

visit::walk_generics(this, generics);
visit::walk_fn_decl(this, decl);

let closure_def = this.create_def(closure_id,
DefPathData::ClosureExpr,
REGULAR_SPACE,
span);
this.with_parent(closure_def, visit_fn)
this.with_parent(closure_def, |this| {
visit::walk_block(this, body);
})
})
}

Expand Down Expand Up @@ -122,17 +137,20 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
return visit::walk_item(self, i);
}
ItemKind::Fn(_, FnHeader { asyncness: IsAsync::Async {
closure_id,
return_impl_trait_id,
}, .. }, ..) => {
ItemKind::Fn(
ref decl,
ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
ref generics,
ref body,
) => {
return self.visit_async_fn(
i.id,
closure_id,
return_impl_trait_id,
i.ident.name,
i.span,
|this| visit::walk_item(this, i)
header,
generics,
decl,
body,
)
}
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
Expand Down Expand Up @@ -233,18 +251,17 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
let def_data = match ii.node {
ImplItemKind::Method(MethodSig {
header: FnHeader { asyncness: IsAsync::Async {
closure_id,
return_impl_trait_id,
}, .. }, ..
}, ..) => {
header: ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
ref decl,
}, ref body) => {
return self.visit_async_fn(
ii.id,
closure_id,
return_impl_trait_id,
ii.ident.name,
ii.span,
|this| visit::walk_impl_item(this, ii)
header,
&ii.generics,
decl,
body,
)
}
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
Expand Down
21 changes: 0 additions & 21 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,27 +602,6 @@ impl<'hir> Map<'hir> {
}
}

pub fn ty_param_owner(&self, id: NodeId) -> NodeId {
match self.get(id) {
NodeItem(&Item { node: ItemKind::Trait(..), .. }) => id,
NodeGenericParam(_) => self.get_parent_node(id),
_ => {
bug!("ty_param_owner: {} not a type parameter",
self.node_to_string(id))
}
}
}

pub fn ty_param_name(&self, id: NodeId) -> Name {
match self.get(id) {
NodeItem(&Item { node: ItemKind::Trait(..), .. }) => {
keywords::SelfType.name()
}
NodeGenericParam(param) => param.name.ident().name,
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
}
}

pub fn trait_impls(&self, trait_did: DefId) -> &'hir [NodeId] {
self.dep_graph.read(DepNode::new_no_params(DepKind::AllLocalTraitImpls));

Expand Down
15 changes: 6 additions & 9 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,16 @@ for ty::RegionKind {
db.hash_stable(hcx, hasher);
i.hash_stable(hcx, hasher);
}
ty::ReLateBound(db, ty::BrNamed(def_id, name)) => {
ty::ReLateBound(db, ty::BrNamed(def_id)) => {
db.hash_stable(hcx, hasher);
def_id.hash_stable(hcx, hasher);
name.hash_stable(hcx, hasher);
}
ty::ReLateBound(db, ty::BrEnv) => {
db.hash_stable(hcx, hasher);
}
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
ty::ReEarlyBound(ty::GenericParam { def_id, index }) => {
def_id.hash_stable(hcx, hasher);
index.hash_stable(hcx, hasher);
name.hash_stable(hcx, hasher);
}
ty::ReScope(scope) => {
scope.hash_stable(hcx, hasher);
Expand Down Expand Up @@ -692,7 +690,6 @@ impl_stable_hash_for!(struct ty::Generics {
});

impl_stable_hash_for!(struct ty::GenericParamDef {
name,
def_id,
index,
pure_wrt_drop,
Expand Down Expand Up @@ -796,7 +793,7 @@ impl_stable_hash_for!(struct ty::FreeRegion {

impl_stable_hash_for!(enum ty::BoundRegion {
BrAnon(index),
BrNamed(def_id, name),
BrNamed(def_id),
BrFresh(index),
BrEnv
});
Expand Down Expand Up @@ -938,9 +935,9 @@ for ty::FloatVid
}
}

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

impl_stable_hash_for!(struct ty::TypeAndMut<'tcx> {
Expand Down
65 changes: 30 additions & 35 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
};
let (prefix, span) = match *region {
ty::ReEarlyBound(ref br) => {
let name = self.generic_param_name(br.def_id);
let mut sp = cm.def_span(self.hir.span(node));
if let Some(param) = self.hir.get_generics(scope).and_then(|generics| {
generics.get_named(&br.name)
generics.get_named(&name)
}) {
sp = param.span;
}
(format!("the lifetime {} as defined on", br.name), sp)
(format!("the lifetime {} as defined on", name), sp)
}
ty::ReFree(ty::FreeRegion {
bound_region: ty::BoundRegion::BrNamed(_, ref name), ..
bound_region: ty::BoundRegion::BrNamed(def_id), ..
}) => {
let name = self.generic_param_name(def_id);
let mut sp = cm.def_span(self.hir.span(node));
if let Some(param) = self.hir.get_generics(scope).and_then(|generics| {
generics.get_named(&name)
Expand Down Expand Up @@ -1112,37 +1114,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 Expand Up @@ -1346,7 +1338,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
br_string(br),
self.tcx.associated_item(def_id).ident
),
infer::EarlyBoundRegion(_, name) => format!(" for lifetime parameter `{}`", name),
infer::EarlyBoundRegion(_, def_id) => format!(
" for lifetime parameter `{}`",
self.tcx.generic_param_name(def_id),
),
infer::BoundRegionInCoherence(name) => {
format!(" for lifetime parameter `{}` in coherence check", name)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
pub fn extract_type_name(&self, ty: &'a Ty<'tcx>) -> String {
if let ty::Infer(ty::TyVar(ty_vid)) = (*ty).sty {
let ty_vars = self.type_variables.borrow();
if let TypeVariableOrigin::TypeParameterDefinition(_, name) =
if let TypeVariableOrigin::TypeParameterDefinition(_, def_id) =
*ty_vars.var_origin(ty_vid) {
name.to_string()
self.tcx.generic_param_name(def_id).to_string()
} else {
ty.to_string()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
// Find the index of the named region that was part of the
// error. We will then search the function parameters for a bound
// region at the right depth with the same index
(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id)) => {
debug!(
"EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
def_id={:?}",
Expand All @@ -166,7 +166,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
// region at the right depth with the same index
(
Some(rl::Region::LateBound(debruijn_index, id, _)),
ty::BrNamed(def_id, _),
ty::BrNamed(def_id),
) => {
debug!(
"FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> {
}
}

(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id)) => {
debug!(
"EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
def_id={:?}",
Expand All @@ -254,7 +254,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> {
}
}

(Some(rl::Region::LateBound(debruijn_index, id, _)), ty::BrNamed(def_id, _)) => {
(Some(rl::Region::LateBound(debruijn_index, id, _)), ty::BrNamed(def_id)) => {
debug!(
"FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
debruijn_index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
ty::BrNamed(..) => true,
_ => false,
},
ty::ReEarlyBound(ebr) => ebr.has_name(),
ty::ReEarlyBound(ebr) => ebr.has_name(self.tcx),
_ => false,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {

let lifetime_name = match sup_r {
RegionKind::ReFree(FreeRegion {
bound_region: BoundRegion::BrNamed(_, ref name), ..
}) => name.to_string(),
bound_region: BoundRegion::BrNamed(def_id), ..
}) => self.tcx.generic_param_name(*def_id).to_string(),
_ => "'_".to_owned(),
};
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(return_sp) {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting/nice_region_error/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
ty::ReEarlyBound(ref ebr) => (
self.tcx.parent_def_id(ebr.def_id).unwrap(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
ty::BoundRegion::BrNamed(ebr.def_id),
),
_ => return None, // not a free region
};
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
ty::ReEarlyBound(ref ebr) => (
self.tcx.parent_def_id(ebr.def_id).unwrap(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
ty::BoundRegion::BrNamed(ebr.def_id),
),
_ => return None, // not a free region
};
Expand Down
Loading