Skip to content

Commit

Permalink
add changes for confusing type params
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurikholkar committed Jan 21, 2018
1 parent 15a1e28 commit 4ba359b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ for ty::TypeVariants<'gcx>

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

impl_stable_hash_for!(struct ty::TypeAndMut<'tcx> {
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 @@ -221,7 +221,7 @@ impl<'cx, 'gcx, 'tcx> Elaborator<'cx, 'gcx, 'tcx> {
},

Component::Param(p) => {
let ty = tcx.mk_param(p.idx, p.name);
let ty = tcx.mk_param(p.idx, p.name, p.span);
Some(ty::Predicate::TypeOutlives(
ty::Binder(ty::OutlivesPredicate(ty, r_min))))
},
Expand Down
12 changes: 7 additions & 5 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use syntax::ast::{self, Name, NodeId};
use syntax::attr;
use syntax::codemap::MultiSpan;
use syntax::symbol::{Symbol, keywords};
use syntax_pos::Span;
use syntax_pos::{Span, DUMMY_SP};

use hir;

Expand Down Expand Up @@ -2095,18 +2095,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.mk_ty(TyInfer(it))
}


pub fn mk_param(self,
index: u32,
name: Name) -> Ty<'tcx> {
self.mk_ty(TyParam(ParamTy { idx: index, name: name }))
name: Name,
span: Span) -> Ty<'tcx> {
self.mk_ty(TyParam(ParamTy { idx: index, name: name, span:span }))
}

pub fn mk_self_type(self) -> Ty<'tcx> {
self.mk_param(0, keywords::SelfType.name())
self.mk_param(0, keywords::SelfType.name(), DUMMY_SP)
}

pub fn mk_param_from_def(self, def: &ty::TypeParameterDef) -> Ty<'tcx> {
self.mk_param(def.index, def.name)
self.mk_param(def.index, def.name, DUMMY_SP)
}

pub fn mk_anon(self, def_id: DefId, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
Expand Down
24 changes: 16 additions & 8 deletions src/librustc/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
if p.is_self() {
"Self".to_string()
} else {
"type parameter".to_string()
p.to_string()
}
}
ty::TyAnon(..) => "anonymized type".to_string(),
Expand All @@ -256,13 +256,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
use self::TypeError::*;

match err.clone() {
Sorts(values) => {
let expected_str = values.expected.sort_string(self);
let found_str = values.found.sort_string(self);
if expected_str == found_str && expected_str == "closure" {
db.note("no two closures, even if identical, have the same type");
db.help("consider boxing your closure and/or using it as a trait object");
}
Sorts(values)
=> {

This comment has been minimized.

Copy link
@estebank

estebank Jan 23, 2018

remove prior newline

debug!("values = {:?}", values);
match values
{

This comment has been minimized.

Copy link
@estebank

estebank Jan 23, 2018

remove prior newline

(ty.sty:TypeVariants::Param(param), _) |
(_, ty.sty:TypeVariants::Param(param)) => {
let expected_str = values.expected.sort_string(self);
let found_str = values.found.sort_string(self);
if expected_str == found_str && expected_str == "closure" {
db.note("no two closures, even if identical, have the same type");
db.help("consider boxing your closure and/or using it as a trait object");
}

This comment has been minimized.

Copy link
@estebank

estebank Jan 23, 2018

This should be

(Ty { sty: TypeVariants::Param(_), .. }, _) | (_, Ty { sty: TypeVariants::Param(_), .. }) => {

ty nor param are being used anywhere. In order to match on a field of a struct you have to "destructure" the struct into it's fields. You can also use match x { y if y == z => (), } in some cases.

},
_=> {}}
},
TyParamDefaultMismatch(values) => {
let expected = values.expected;
Expand Down
12 changes: 7 additions & 5 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::cmp::Ordering;
use syntax::abi;
use syntax::ast::{self, Name};
use syntax::symbol::keywords;
use syntax_pos::{Span, DUMMY_SP};

use serialize;

Expand Down Expand Up @@ -862,23 +863,24 @@ impl<'tcx> PolyFnSig<'tcx> {
pub struct ParamTy {
pub idx: u32,
pub name: Name,
pub span: Span
}

impl<'a, 'gcx, 'tcx> ParamTy {
pub fn new(index: u32, name: Name) -> ParamTy {
ParamTy { idx: index, name: name }
pub fn new(index: u32, name: Name, span: Span) -> ParamTy {
ParamTy { idx: index, name: name, span: span }
}

pub fn for_self() -> ParamTy {
ParamTy::new(0, keywords::SelfType.name())
ParamTy::new(0, keywords::SelfType.name(), DUMMY_SP)
}

pub fn for_def(def: &ty::TypeParameterDef) -> ParamTy {
ParamTy::new(def.index, def.name)
ParamTy::new(def.index, def.name, DUMMY_SP)
}

pub fn to_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
tcx.mk_param(self.idx, self.name)
tcx.mk_param(self.idx, self.name, self.span)
}

pub fn is_self(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let item_def_id = tcx.hir.local_def_id(item_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.type_param_to_index[&tcx.hir.local_def_id(node_id).index];
tcx.mk_param(index, tcx.hir.name(node_id))
tcx.mk_param(index, tcx.hir.name(node_id), span)
}
Def::SelfTy(_, Some(def_id)) => {
// Self in impl (we know the concrete type).
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn equate_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
/// and in libcore/intrinsics.rs
pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
it: &hir::ForeignItem) {
let param = |n| tcx.mk_param(n, Symbol::intern(&format!("P{}", n)));
let param = |n| tcx.mk_param(n, Symbol::intern(&format!("P{}", n)), it.span);
let name = it.name.as_str();
let (n_tps, inputs, output) = if name.starts_with("atomic_") {
let split : Vec<&str> = name.split('_').collect();
Expand Down Expand Up @@ -341,7 +341,7 @@ pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
it: &hir::ForeignItem) {
let param = |n| {
let name = Symbol::intern(&format!("P{}", n));
tcx.mk_param(n, name)
tcx.mk_param(n, name, it.span)
};

let def_id = tcx.hir.local_def_id(it.id);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn type_param_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let param_owner_def_id = tcx.hir.local_def_id(param_owner);
let generics = tcx.generics_of(param_owner_def_id);
let index = generics.type_param_to_index[&def_id.index];
let ty = tcx.mk_param(index, tcx.hir.ty_param_name(param_id));
let ty = tcx.mk_param(index, tcx.hir.ty_param_name(param_id), DUMMY_SP);

// Don't look for bounds where the type parameter isn't in scope.
let parent = if item_def_id == param_owner_def_id {
Expand Down Expand Up @@ -1477,7 +1477,7 @@ fn explicit_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// Collect the predicates that were written inline by the user on each
// type parameter (e.g., `<T:Foo>`).
for param in ast_generics.ty_params() {
let param_ty = ty::ParamTy::new(index, param.name).to_ty(tcx);
let param_ty = ty::ParamTy::new(index, param.name, param.span).to_ty(tcx);
index += 1;

let bounds = compute_bounds(&icx,
Expand Down

0 comments on commit 4ba359b

Please sign in to comment.