Skip to content

Commit e0a1ab6

Browse files
committedSep 29, 2018
Auto merge of #54591 - ljedrz:cleanup_typeck_rest, r=zackmdavis
A few cleanups and minor improvements to typeck This PR complements #54533, which was limited to `check`. - change a few `push` loops to `extend`s - prefer `to_owned` to `to_string` for string literals - prefer `if let` to `match` where only one branch matters - a few other minor improvements - whitespace fixes
·
1.88.01.31.0
2 parents bb0896a + 608c395 commit e0a1ab6

File tree

15 files changed

+344
-392
lines changed

15 files changed

+344
-392
lines changed
 

‎src/librustc_typeck/astconv.rs

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
147147
bound_region: ty::BrNamed(id, name)
148148
}))
149149

150-
// (*) -- not late-bound, won't change
150+
// (*) -- not late-bound, won't change
151151
}
152152

153153
None => {
@@ -167,8 +167,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
167167
};
168168

169169
debug!("ast_region_to_region(lifetime={:?}) yields {:?}",
170-
lifetime,
171-
r);
170+
lifetime,
171+
r);
172172

173173
r
174174
}
@@ -218,7 +218,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
218218
span,
219219
E0632,
220220
"cannot provide explicit type parameters when `impl Trait` is \
221-
used in argument position."
221+
used in argument position."
222222
};
223223

224224
err.emit();
@@ -538,7 +538,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
538538
// region with the current anon region binding (in other words,
539539
// whatever & would get replaced with).
540540
debug!("create_substs_for_ast_path(def_id={:?}, self_ty={:?}, \
541-
generic_args={:?})",
541+
generic_args={:?})",
542542
def_id, self_ty, generic_args);
543543

544544
let tcx = self.tcx();
@@ -609,7 +609,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
609609
if default_needs_object_self(param) {
610610
struct_span_err!(tcx.sess, span, E0393,
611611
"the type parameter `{}` must be explicitly \
612-
specified",
612+
specified",
613613
param.name)
614614
.span_label(span,
615615
format!("missing reference to `{}`", param.name))
@@ -623,7 +623,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
623623
self.normalize_ty(
624624
span,
625625
tcx.at(span).type_of(param.def_id)
626-
.subst_spanned(tcx, substs.unwrap(), Some(span))
626+
.subst_spanned(tcx, substs.unwrap(), Some(span))
627627
).into()
628628
}
629629
} else if infer_types {
@@ -851,7 +851,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
851851
binding.span,
852852
E0582,
853853
"binding for associated type `{}` references lifetime `{}`, \
854-
which does not appear in the trait input types",
854+
which does not appear in the trait input types",
855855
binding.item_name, br_name)
856856
.emit();
857857
}
@@ -891,7 +891,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
891891
ref_id,
892892
binding.span,
893893
&format!("associated type binding `{}` specified more than once",
894-
binding.item_name)
894+
binding.item_name)
895895
);
896896
err.span_label(binding.span, "used more than once");
897897
err.span_label(*prev_span, format!("first use of `{}`", binding.item_name));
@@ -994,7 +994,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
994994
if !object_safety_violations.is_empty() {
995995
tcx.report_object_safety_error(
996996
span, principal.def_id(), object_safety_violations)
997-
.emit();
997+
.emit();
998998
return tcx.types.err;
999999
}
10001000

@@ -1014,13 +1014,13 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
10141014
for item_def_id in associated_types {
10151015
let assoc_item = tcx.associated_item(item_def_id);
10161016
let trait_def_id = assoc_item.container.id();
1017-
struct_span_err!(tcx.sess, span, E0191,
1018-
"the value of the associated type `{}` (from the trait `{}`) must be specified",
1019-
assoc_item.ident,
1020-
tcx.item_path_str(trait_def_id))
1021-
.span_label(span, format!(
1022-
"missing associated type `{}` value", assoc_item.ident))
1023-
.emit();
1017+
struct_span_err!(tcx.sess, span, E0191, "the value of the associated type `{}` \
1018+
(from the trait `{}`) must be specified",
1019+
assoc_item.ident,
1020+
tcx.item_path_str(trait_def_id))
1021+
.span_label(span, format!("missing associated type `{}` value",
1022+
assoc_item.ident))
1023+
.emit();
10241024
}
10251025

10261026
// Dedup auto traits so that `dyn Trait + Send + Send` is the same as `dyn Trait + Send`.
@@ -1032,12 +1032,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
10321032
iter::once(ty::ExistentialPredicate::Trait(*existential_principal.skip_binder()))
10331033
.chain(auto_traits.into_iter().map(ty::ExistentialPredicate::AutoTrait))
10341034
.chain(existential_projections
1035-
.map(|x| ty::ExistentialPredicate::Projection(*x.skip_binder())))
1035+
.map(|x| ty::ExistentialPredicate::Projection(*x.skip_binder())))
10361036
.collect::<SmallVec<[_; 8]>>();
10371037
v.sort_by(|a, b| a.stable_cmp(tcx, b));
10381038
let existential_predicates = ty::Binder::bind(tcx.mk_existential_predicates(v.into_iter()));
10391039

1040-
10411040
// Explicitly specified region bound. Use that.
10421041
let region_bound = if !lifetime.is_elided() {
10431042
self.ast_region_to_region(lifetime, None)
@@ -1072,7 +1071,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
10721071
struct_span_err!(self.tcx().sess, span, E0223, "ambiguous associated type")
10731072
.span_label(span, "ambiguous associated type")
10741073
.note(&format!("specify the type using the syntax `<{} as {}>::{}`",
1075-
type_str, trait_str, name))
1074+
type_str, trait_str, name))
10761075
.emit();
10771076

10781077
}
@@ -1094,8 +1093,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
10941093

10951094
// Check that there is exactly one way to find an associated type with the
10961095
// correct name.
1097-
let suitable_bounds =
1098-
traits::transitive_bounds(tcx, &bounds)
1096+
let suitable_bounds = traits::transitive_bounds(tcx, &bounds)
10991097
.filter(|b| self.trait_defines_associated_type_named(b.def_id(), assoc_name));
11001098

11011099
let param_node_id = tcx.hir.as_local_node_id(ty_param_def_id).unwrap();
@@ -1110,20 +1108,20 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
11101108
// Checks that bounds contains exactly one element and reports appropriate
11111109
// errors otherwise.
11121110
fn one_bound_for_assoc_type<I>(&self,
1113-
mut bounds: I,
1114-
ty_param_name: &str,
1115-
assoc_name: ast::Ident,
1116-
span: Span)
1111+
mut bounds: I,
1112+
ty_param_name: &str,
1113+
assoc_name: ast::Ident,
1114+
span: Span)
11171115
-> Result<ty::PolyTraitRef<'tcx>, ErrorReported>
11181116
where I: Iterator<Item=ty::PolyTraitRef<'tcx>>
11191117
{
11201118
let bound = match bounds.next() {
11211119
Some(bound) => bound,
11221120
None => {
11231121
struct_span_err!(self.tcx().sess, span, E0220,
1124-
"associated type `{}` not found for `{}`",
1125-
assoc_name,
1126-
ty_param_name)
1122+
"associated type `{}` not found for `{}`",
1123+
assoc_name,
1124+
ty_param_name)
11271125
.span_label(span, format!("associated type `{}` not found", assoc_name))
11281126
.emit();
11291127
return Err(ErrorReported);
@@ -1142,14 +1140,14 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
11421140
for bound in bounds {
11431141
let bound_span = self.tcx().associated_items(bound.def_id()).find(|item| {
11441142
item.kind == ty::AssociatedKind::Type &&
1145-
self.tcx().hygienic_eq(assoc_name, item.ident, bound.def_id())
1143+
self.tcx().hygienic_eq(assoc_name, item.ident, bound.def_id())
11461144
})
11471145
.and_then(|item| self.tcx().hir.span_if_local(item.def_id));
11481146

11491147
if let Some(span) = bound_span {
11501148
err.span_label(span, format!("ambiguous `{}` from `{}`",
1151-
assoc_name,
1152-
bound));
1149+
assoc_name,
1150+
bound));
11531151
} else {
11541152
span_note!(&mut err, span,
11551153
"associated type `{}` could derive from `{}`",
@@ -1198,8 +1196,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
11981196
}
11991197
};
12001198

1201-
let candidates =
1202-
traits::supertraits(tcx, ty::Binder::bind(trait_ref))
1199+
let candidates = traits::supertraits(tcx, ty::Binder::bind(trait_ref))
12031200
.filter(|r| self.trait_defines_associated_type_named(r.def_id(), assoc_name));
12041201

12051202
match self.one_bound_for_assoc_type(candidates, "Self", assoc_name, span) {
@@ -1230,7 +1227,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
12301227
let (assoc_ident, def_scope) = tcx.adjust_ident(assoc_name, trait_did, ref_id);
12311228
let item = tcx.associated_items(trait_did).find(|i| {
12321229
Namespace::from(i.kind) == Namespace::Type &&
1233-
i.ident.modern() == assoc_ident
1230+
i.ident.modern() == assoc_ident
12341231
})
12351232
.expect("missing associated type");
12361233

@@ -1293,16 +1290,15 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
12931290
if err_for_lt { continue }
12941291
err_for_lt = true;
12951292
(struct_span_err!(self.tcx().sess, lt.span, E0110,
1296-
"lifetime parameters are not allowed on \
1297-
this type"),
1293+
"lifetime parameters are not allowed on this type"),
12981294
lt.span,
12991295
"lifetime")
13001296
}
13011297
hir::GenericArg::Type(ty) => {
13021298
if err_for_ty { continue }
13031299
err_for_ty = true;
13041300
(struct_span_err!(self.tcx().sess, ty.span, E0109,
1305-
"type parameters are not allowed on this type"),
1301+
"type parameters are not allowed on this type"),
13061302
ty.span,
13071303
"type")
13081304
}
@@ -1590,7 +1586,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
15901586
));
15911587

15921588
// Find any late-bound regions declared in return type that do
1593-
// not appear in the arguments. These are not wellformed.
1589+
// not appear in the arguments. These are not well-formed.
15941590
//
15951591
// Example:
15961592
// for<'a> fn() -> &'a str <-- 'a is bad

‎src/librustc_typeck/check_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, 'tcx> CheckVisitor<'a, 'tcx> {
7575
let msg = if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
7676
format!("unused import: `{}`", snippet)
7777
} else {
78-
"unused import".to_string()
78+
"unused import".to_owned()
7979
};
8080
self.tcx.lint_node(lint::builtin::UNUSED_IMPORTS, id, span, &msg);
8181
}

‎src/librustc_typeck/coherence/builtin.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,29 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
5555
}
5656

5757
fn visit_implementation_of_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl_did: DefId) {
58-
match tcx.type_of(impl_did).sty {
59-
ty::Adt(..) => {}
60-
_ => {
61-
// Destructors only work on nominal types.
62-
if let Some(impl_node_id) = tcx.hir.as_local_node_id(impl_did) {
63-
match tcx.hir.find(impl_node_id) {
64-
Some(Node::Item(item)) => {
65-
let span = match item.node {
66-
ItemKind::Impl(.., ref ty, _) => ty.span,
67-
_ => item.span,
68-
};
69-
struct_span_err!(tcx.sess,
70-
span,
71-
E0120,
72-
"the Drop trait may only be implemented on \
73-
structures")
74-
.span_label(span, "implementing Drop requires a struct")
75-
.emit();
76-
}
77-
_ => {
78-
bug!("didn't find impl in ast map");
79-
}
80-
}
58+
if let ty::Adt(..) = tcx.type_of(impl_did).sty {
59+
/* do nothing */
60+
} else {
61+
// Destructors only work on nominal types.
62+
if let Some(impl_node_id) = tcx.hir.as_local_node_id(impl_did) {
63+
if let Some(Node::Item(item)) = tcx.hir.find(impl_node_id) {
64+
let span = match item.node {
65+
ItemKind::Impl(.., ref ty, _) => ty.span,
66+
_ => item.span,
67+
};
68+
struct_span_err!(tcx.sess,
69+
span,
70+
E0120,
71+
"the Drop trait may only be implemented on \
72+
structures")
73+
.span_label(span, "implementing Drop requires a struct")
74+
.emit();
8175
} else {
82-
bug!("found external impl of Drop trait on \
83-
something other than a struct");
76+
bug!("didn't find impl in ast map");
8477
}
78+
} else {
79+
bug!("found external impl of Drop trait on \
80+
something other than a struct");
8581
}
8682
}
8783
}
@@ -92,8 +88,7 @@ fn visit_implementation_of_copy<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl_did:
9288
let impl_node_id = if let Some(n) = tcx.hir.as_local_node_id(impl_did) {
9389
n
9490
} else {
95-
debug!("visit_implementation_of_copy(): impl not in this \
96-
crate");
91+
debug!("visit_implementation_of_copy(): impl not in this crate");
9792
return;
9893
};
9994

@@ -119,11 +114,11 @@ fn visit_implementation_of_copy<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl_did:
119114
};
120115

121116
let mut err = struct_span_err!(tcx.sess,
122-
span,
123-
E0204,
124-
"the trait `Copy` may not be implemented for this type");
117+
span,
118+
E0204,
119+
"the trait `Copy` may not be implemented for this type");
125120
for span in fields.iter().map(|f| tcx.def_span(f.did)) {
126-
err.span_label(span, "this field does not implement `Copy`");
121+
err.span_label(span, "this field does not implement `Copy`");
127122
}
128123
err.emit()
129124
}
@@ -173,12 +168,9 @@ pub fn coerce_unsized_info<'a, 'gcx>(gcx: TyCtxt<'a, 'gcx, 'gcx>,
173168
debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did);
174169
let coerce_unsized_trait = gcx.lang_items().coerce_unsized_trait().unwrap();
175170

176-
let unsize_trait = match gcx.lang_items().require(UnsizeTraitLangItem) {
177-
Ok(id) => id,
178-
Err(err) => {
179-
gcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err));
180-
}
181-
};
171+
let unsize_trait = gcx.lang_items().require(UnsizeTraitLangItem).unwrap_or_else(|err| {
172+
gcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err));
173+
});
182174

183175
// this provider should only get invoked for local def-ids
184176
let impl_node_id = gcx.hir.as_local_node_id(impl_did).unwrap_or_else(|| {
@@ -210,9 +202,9 @@ pub fn coerce_unsized_info<'a, 'gcx>(gcx: TyCtxt<'a, 'gcx, 'gcx>,
210202
mk_ptr: &dyn Fn(Ty<'gcx>) -> Ty<'gcx>| {
211203
if (mt_a.mutbl, mt_b.mutbl) == (hir::MutImmutable, hir::MutMutable) {
212204
infcx.report_mismatched_types(&cause,
213-
mk_ptr(mt_b.ty),
214-
target,
215-
ty::error::TypeError::Mutability)
205+
mk_ptr(mt_b.ty),
206+
target,
207+
ty::error::TypeError::Mutability)
216208
.emit();
217209
}
218210
(mt_a.ty, mt_b.ty, unsize_trait, None)
@@ -235,7 +227,7 @@ pub fn coerce_unsized_info<'a, 'gcx>(gcx: TyCtxt<'a, 'gcx, 'gcx>,
235227
}
236228

237229
(&ty::Adt(def_a, substs_a), &ty::Adt(def_b, substs_b)) if def_a.is_struct() &&
238-
def_b.is_struct() => {
230+
def_b.is_struct() => {
239231
if def_a != def_b {
240232
let source_path = gcx.item_path_str(def_a.did);
241233
let target_path = gcx.item_path_str(def_b.did);

‎src/librustc_typeck/coherence/inherent_impls.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ impl<'a, 'tcx> InherentCollect<'a, 'tcx> {
315315
E0116,
316316
"cannot define inherent `impl` for a type outside of the crate \
317317
where the type is defined")
318-
.span_label(item.span,
319-
"impl for type defined outside of crate.")
318+
.span_label(item.span, "impl for type defined outside of crate.")
320319
.note("define and implement a trait or new type instead")
321320
.emit();
322321
}

‎src/librustc_typeck/coherence/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ fn check_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeId) {
3636

3737
if let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) {
3838
debug!("(checking implementation) adding impl for trait '{:?}', item '{}'",
39-
trait_ref,
40-
tcx.item_path_str(impl_def_id));
39+
trait_ref,
40+
tcx.item_path_str(impl_def_id));
4141

4242
// Skip impls where one of the self type is an error type.
4343
// This occurs with e.g. resolve failures (#30589).

‎src/librustc_typeck/coherence/orphan.rs

Lines changed: 113 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -33,131 +33,126 @@ impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for OrphanChecker<'cx, 'tcx> {
3333
/// reports.
3434
fn visit_item(&mut self, item: &hir::Item) {
3535
let def_id = self.tcx.hir.local_def_id(item.id);
36-
match item.node {
37-
hir::ItemKind::Impl(.., Some(_), _, _) => {
38-
// "Trait" impl
39-
debug!("coherence2::orphan check: trait impl {}",
40-
self.tcx.hir.node_to_string(item.id));
41-
let trait_ref = self.tcx.impl_trait_ref(def_id).unwrap();
42-
let trait_def_id = trait_ref.def_id;
43-
let cm = self.tcx.sess.source_map();
44-
let sp = cm.def_span(item.span);
45-
match traits::orphan_check(self.tcx, def_id) {
46-
Ok(()) => {}
47-
Err(traits::OrphanCheckErr::NoLocalInputType) => {
48-
struct_span_err!(self.tcx.sess,
49-
sp,
50-
E0117,
51-
"only traits defined in the current crate can be \
52-
implemented for arbitrary types")
53-
.span_label(sp, "impl doesn't use types inside crate")
54-
.note("the impl does not reference any types defined in this crate")
55-
.note("define and implement a trait or new type instead")
56-
.emit();
57-
return;
58-
}
59-
Err(traits::OrphanCheckErr::UncoveredTy(param_ty)) => {
60-
struct_span_err!(self.tcx.sess,
61-
sp,
62-
E0210,
63-
"type parameter `{}` must be used as the type parameter \
64-
for some local type (e.g. `MyStruct<{}>`)",
65-
param_ty,
66-
param_ty)
67-
.span_label(sp,
68-
format!("type parameter `{}` must be used as the type \
69-
parameter for some local type", param_ty))
70-
.note("only traits defined in the current crate can be implemented \
71-
for a type parameter")
72-
.emit();
73-
return;
74-
}
36+
// "Trait" impl
37+
if let hir::ItemKind::Impl(.., Some(_), _, _) = item.node {
38+
debug!("coherence2::orphan check: trait impl {}",
39+
self.tcx.hir.node_to_string(item.id));
40+
let trait_ref = self.tcx.impl_trait_ref(def_id).unwrap();
41+
let trait_def_id = trait_ref.def_id;
42+
let cm = self.tcx.sess.source_map();
43+
let sp = cm.def_span(item.span);
44+
match traits::orphan_check(self.tcx, def_id) {
45+
Ok(()) => {}
46+
Err(traits::OrphanCheckErr::NoLocalInputType) => {
47+
struct_span_err!(self.tcx.sess,
48+
sp,
49+
E0117,
50+
"only traits defined in the current crate can be \
51+
implemented for arbitrary types")
52+
.span_label(sp, "impl doesn't use types inside crate")
53+
.note("the impl does not reference any types defined in this crate")
54+
.note("define and implement a trait or new type instead")
55+
.emit();
56+
return;
57+
}
58+
Err(traits::OrphanCheckErr::UncoveredTy(param_ty)) => {
59+
struct_span_err!(self.tcx.sess,
60+
sp,
61+
E0210,
62+
"type parameter `{}` must be used as the type parameter \
63+
for some local type (e.g. `MyStruct<{}>`)",
64+
param_ty,
65+
param_ty)
66+
.span_label(sp,
67+
format!("type parameter `{}` must be used as the type \
68+
parameter for some local type", param_ty))
69+
.note("only traits defined in the current crate can be implemented \
70+
for a type parameter")
71+
.emit();
72+
return;
7573
}
74+
}
7675

77-
// In addition to the above rules, we restrict impls of auto traits
78-
// so that they can only be implemented on nominal types, such as structs,
79-
// enums or foreign types. To see why this restriction exists, consider the
80-
// following example (#22978). Imagine that crate A defines an auto trait
81-
// `Foo` and a fn that operates on pairs of types:
82-
//
83-
// ```
84-
// // Crate A
85-
// auto trait Foo { }
86-
// fn two_foos<A:Foo,B:Foo>(..) {
87-
// one_foo::<(A,B)>(..)
88-
// }
89-
// fn one_foo<T:Foo>(..) { .. }
90-
// ```
91-
//
92-
// This type-checks fine; in particular the fn
93-
// `two_foos` is able to conclude that `(A,B):Foo`
94-
// because `A:Foo` and `B:Foo`.
95-
//
96-
// Now imagine that crate B comes along and does the following:
97-
//
98-
// ```
99-
// struct A { }
100-
// struct B { }
101-
// impl Foo for A { }
102-
// impl Foo for B { }
103-
// impl !Send for (A, B) { }
104-
// ```
105-
//
106-
// This final impl is legal according to the orpan
107-
// rules, but it invalidates the reasoning from
108-
// `two_foos` above.
109-
debug!("trait_ref={:?} trait_def_id={:?} trait_is_auto={}",
110-
trait_ref,
111-
trait_def_id,
112-
self.tcx.trait_is_auto(trait_def_id));
113-
if self.tcx.trait_is_auto(trait_def_id) &&
114-
!trait_def_id.is_local() {
115-
let self_ty = trait_ref.self_ty();
116-
let opt_self_def_id = match self_ty.sty {
117-
ty::Adt(self_def, _) => Some(self_def.did),
118-
ty::Foreign(did) => Some(did),
119-
_ => None,
120-
};
76+
// In addition to the above rules, we restrict impls of auto traits
77+
// so that they can only be implemented on nominal types, such as structs,
78+
// enums or foreign types. To see why this restriction exists, consider the
79+
// following example (#22978). Imagine that crate A defines an auto trait
80+
// `Foo` and a fn that operates on pairs of types:
81+
//
82+
// ```
83+
// // Crate A
84+
// auto trait Foo { }
85+
// fn two_foos<A:Foo,B:Foo>(..) {
86+
// one_foo::<(A,B)>(..)
87+
// }
88+
// fn one_foo<T:Foo>(..) { .. }
89+
// ```
90+
//
91+
// This type-checks fine; in particular the fn
92+
// `two_foos` is able to conclude that `(A,B):Foo`
93+
// because `A:Foo` and `B:Foo`.
94+
//
95+
// Now imagine that crate B comes along and does the following:
96+
//
97+
// ```
98+
// struct A { }
99+
// struct B { }
100+
// impl Foo for A { }
101+
// impl Foo for B { }
102+
// impl !Send for (A, B) { }
103+
// ```
104+
//
105+
// This final impl is legal according to the orpan
106+
// rules, but it invalidates the reasoning from
107+
// `two_foos` above.
108+
debug!("trait_ref={:?} trait_def_id={:?} trait_is_auto={}",
109+
trait_ref,
110+
trait_def_id,
111+
self.tcx.trait_is_auto(trait_def_id));
112+
if self.tcx.trait_is_auto(trait_def_id) &&
113+
!trait_def_id.is_local() {
114+
let self_ty = trait_ref.self_ty();
115+
let opt_self_def_id = match self_ty.sty {
116+
ty::Adt(self_def, _) => Some(self_def.did),
117+
ty::Foreign(did) => Some(did),
118+
_ => None,
119+
};
121120

122-
let msg = match opt_self_def_id {
123-
// We only want to permit nominal types, but not *all* nominal types.
124-
// They must be local to the current crate, so that people
125-
// can't do `unsafe impl Send for Rc<SomethingLocal>` or
126-
// `impl !Send for Box<SomethingLocalAndSend>`.
127-
Some(self_def_id) => {
128-
if self_def_id.is_local() {
129-
None
130-
} else {
131-
Some((
132-
format!("cross-crate traits with a default impl, like `{}`, \
133-
can only be implemented for a struct/enum type \
134-
defined in the current crate",
135-
self.tcx.item_path_str(trait_def_id)),
136-
"can't implement cross-crate trait for type in another crate"
137-
))
138-
}
139-
}
140-
_ => {
141-
Some((format!("cross-crate traits with a default impl, like `{}`, can \
142-
only be implemented for a struct/enum type, not `{}`",
143-
self.tcx.item_path_str(trait_def_id),
144-
self_ty),
145-
"can't implement cross-crate trait with a default impl for \
146-
non-struct/enum type"))
121+
let msg = match opt_self_def_id {
122+
// We only want to permit nominal types, but not *all* nominal types.
123+
// They must be local to the current crate, so that people
124+
// can't do `unsafe impl Send for Rc<SomethingLocal>` or
125+
// `impl !Send for Box<SomethingLocalAndSend>`.
126+
Some(self_def_id) => {
127+
if self_def_id.is_local() {
128+
None
129+
} else {
130+
Some((
131+
format!("cross-crate traits with a default impl, like `{}`, \
132+
can only be implemented for a struct/enum type \
133+
defined in the current crate",
134+
self.tcx.item_path_str(trait_def_id)),
135+
"can't implement cross-crate trait for type in another crate"
136+
))
147137
}
148-
};
149-
150-
if let Some((msg, label)) = msg {
151-
struct_span_err!(self.tcx.sess, sp, E0321, "{}", msg)
152-
.span_label(sp, label)
153-
.emit();
154-
return;
155138
}
139+
_ => {
140+
Some((format!("cross-crate traits with a default impl, like `{}`, can \
141+
only be implemented for a struct/enum type, not `{}`",
142+
self.tcx.item_path_str(trait_def_id),
143+
self_ty),
144+
"can't implement cross-crate trait with a default impl for \
145+
non-struct/enum type"))
146+
}
147+
};
148+
149+
if let Some((msg, label)) = msg {
150+
struct_span_err!(self.tcx.sess, sp, E0321, "{}", msg)
151+
.span_label(sp, label)
152+
.emit();
153+
return;
156154
}
157155
}
158-
_ => {
159-
// Not an impl
160-
}
161156
}
162157
}
163158

‎src/librustc_typeck/coherence/unsafety.rs

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,49 @@ impl<'cx, 'tcx, 'v> UnsafetyChecker<'cx, 'tcx> {
2929
item: &'v hir::Item,
3030
impl_generics: Option<&hir::Generics>,
3131
unsafety: hir::Unsafety,
32-
polarity: hir::ImplPolarity) {
33-
match self.tcx.impl_trait_ref(self.tcx.hir.local_def_id(item.id)) {
34-
None => {}
35-
36-
Some(trait_ref) => {
37-
let trait_def = self.tcx.trait_def(trait_ref.def_id);
38-
let unsafe_attr = impl_generics.and_then(|generics| {
39-
generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle")
40-
});
41-
match (trait_def.unsafety, unsafe_attr, unsafety, polarity) {
42-
(Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
43-
span_err!(self.tcx.sess,
44-
item.span,
45-
E0199,
46-
"implementing the trait `{}` is not unsafe",
47-
trait_ref);
48-
}
32+
polarity: hir::ImplPolarity)
33+
{
34+
if let Some(trait_ref) = self.tcx.impl_trait_ref(self.tcx.hir.local_def_id(item.id)) {
35+
let trait_def = self.tcx.trait_def(trait_ref.def_id);
36+
let unsafe_attr = impl_generics.and_then(|generics| {
37+
generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle")
38+
});
39+
match (trait_def.unsafety, unsafe_attr, unsafety, polarity) {
40+
(Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
41+
span_err!(self.tcx.sess,
42+
item.span,
43+
E0199,
44+
"implementing the trait `{}` is not unsafe",
45+
trait_ref);
46+
}
4947

50-
(Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
51-
span_err!(self.tcx.sess,
52-
item.span,
53-
E0200,
54-
"the trait `{}` requires an `unsafe impl` declaration",
55-
trait_ref);
56-
}
48+
(Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
49+
span_err!(self.tcx.sess,
50+
item.span,
51+
E0200,
52+
"the trait `{}` requires an `unsafe impl` declaration",
53+
trait_ref);
54+
}
5755

58-
(Unsafety::Normal, Some(attr_name), Unsafety::Normal,
59-
hir::ImplPolarity::Positive) =>
60-
{
61-
span_err!(self.tcx.sess,
62-
item.span,
63-
E0569,
64-
"requires an `unsafe impl` declaration due to `#[{}]` attribute",
65-
attr_name);
66-
}
56+
(Unsafety::Normal, Some(attr_name), Unsafety::Normal,
57+
hir::ImplPolarity::Positive) =>
58+
{
59+
span_err!(self.tcx.sess,
60+
item.span,
61+
E0569,
62+
"requires an `unsafe impl` declaration due to `#[{}]` attribute",
63+
attr_name);
64+
}
6765

68-
(_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative) => {
69-
// Reported in AST validation
70-
self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
71-
}
72-
(_, _, Unsafety::Normal, hir::ImplPolarity::Negative) |
73-
(Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) |
74-
(Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) |
75-
(Unsafety::Normal, None, Unsafety::Normal, _) => {
76-
// OK
77-
}
66+
(_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative) => {
67+
// Reported in AST validation
68+
self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
69+
}
70+
(_, _, Unsafety::Normal, hir::ImplPolarity::Negative) |
71+
(Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) |
72+
(Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) |
73+
(Unsafety::Normal, None, Unsafety::Normal, _) => {
74+
// OK
7875
}
7976
}
8077
}
@@ -83,11 +80,8 @@ impl<'cx, 'tcx, 'v> UnsafetyChecker<'cx, 'tcx> {
8380

8481
impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for UnsafetyChecker<'cx, 'tcx> {
8582
fn visit_item(&mut self, item: &'v hir::Item) {
86-
match item.node {
87-
hir::ItemKind::Impl(unsafety, polarity, _, ref generics, ..) => {
88-
self.check_unsafety_coherence(item, Some(generics), unsafety, polarity);
89-
}
90-
_ => {}
83+
if let hir::ItemKind::Impl(unsafety, polarity, _, ref generics, ..) = item.node {
84+
self.check_unsafety_coherence(item, Some(generics), unsafety, polarity);
9185
}
9286
}
9387

‎src/librustc_typeck/collect.rs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ use std::iter;
6464
pub fn collect_item_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
6565
let mut visitor = CollectItemTypesVisitor { tcx: tcx };
6666
tcx.hir
67-
.krate()
68-
.visit_all_item_likes(&mut visitor.as_deep_visitor());
67+
.krate()
68+
.visit_all_item_likes(&mut visitor.as_deep_visitor());
6969
}
7070

7171
pub fn provide(providers: &mut Providers) {
@@ -197,7 +197,8 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {
197197
E0121,
198198
"the type placeholder `_` is not allowed within types on item signatures"
199199
).span_label(span, "not allowed in type signatures")
200-
.emit();
200+
.emit();
201+
201202
self.tcx().types.err
202203
}
203204

@@ -529,12 +530,11 @@ fn convert_enum_variant_types<'a, 'tcx>(
529530
).span_label(
530531
variant.span,
531532
format!("overflowed on value after {}", prev_discr.unwrap()),
532-
)
533-
.note(&format!(
534-
"explicitly set `{} = {}` if that is desired outcome",
535-
variant.node.name, wrapped_discr
536-
))
537-
.emit();
533+
).note(&format!(
534+
"explicitly set `{} = {}` if that is desired outcome",
535+
variant.node.name, wrapped_discr
536+
))
537+
.emit();
538538
None
539539
}.unwrap_or(wrapped_discr),
540540
);
@@ -577,8 +577,8 @@ fn convert_variant<'a, 'tcx>(
577577
"field `{}` is already declared",
578578
f.ident
579579
).span_label(f.span, "field already declared")
580-
.span_label(prev_span, format!("`{}` first declared here", f.ident))
581-
.emit();
580+
.span_label(prev_span, format!("`{}` first declared here", f.ident))
581+
.emit();
582582
} else {
583583
seen_fields.insert(f.ident.modern(), f.span);
584584
}
@@ -824,14 +824,11 @@ fn has_late_bound_regions<'a, 'tcx>(
824824
has_late_bound_regions: None,
825825
};
826826
for param in &generics.params {
827-
match param.kind {
828-
GenericParamKind::Lifetime { .. } => {
829-
let hir_id = tcx.hir.node_to_hir_id(param.id);
830-
if tcx.is_late_bound(hir_id) {
831-
return Some(param.span);
832-
}
827+
if let GenericParamKind::Lifetime { .. } = param.kind {
828+
let hir_id = tcx.hir.node_to_hir_id(param.id);
829+
if tcx.is_late_bound(hir_id) {
830+
return Some(param.span);
833831
}
834-
_ => {}
835832
}
836833
}
837834
visitor.visit_fn_decl(decl);
@@ -1314,6 +1311,7 @@ fn find_existential_constraints<'a, 'tcx>(
13141311
def_id: DefId,
13151312
found: Option<(Span, ty::Ty<'tcx>)>,
13161313
}
1314+
13171315
impl<'a, 'tcx> ConstraintLocator<'a, 'tcx> {
13181316
fn check(&mut self, def_id: DefId) {
13191317
trace!("checking {:?}", def_id);
@@ -1347,6 +1345,7 @@ fn find_existential_constraints<'a, 'tcx>(
13471345
}
13481346
}
13491347
}
1348+
13501349
impl<'a, 'tcx> intravisit::Visitor<'tcx> for ConstraintLocator<'a, 'tcx> {
13511350
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
13521351
intravisit::NestedVisitorMap::All(&self.tcx.hir)
@@ -1373,14 +1372,17 @@ fn find_existential_constraints<'a, 'tcx>(
13731372
intravisit::walk_trait_item(self, it);
13741373
}
13751374
}
1375+
13761376
let mut locator = ConstraintLocator {
13771377
def_id,
13781378
tcx,
13791379
found: None,
13801380
};
13811381
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
13821382
let parent = tcx.hir.get_parent(node_id);
1383+
13831384
trace!("parent_id: {:?}", parent);
1385+
13841386
if parent == ast::CRATE_NODE_ID {
13851387
intravisit::walk_crate(&mut locator, tcx.hir.krate());
13861388
} else {
@@ -1395,6 +1397,7 @@ fn find_existential_constraints<'a, 'tcx>(
13951397
),
13961398
}
13971399
}
1400+
13981401
match locator.found {
13991402
Some((_, ty)) => ty,
14001403
None => {
@@ -1786,17 +1789,14 @@ fn explicit_predicates_of<'a, 'tcx>(
17861789
// Collect the predicates that were written inline by the user on each
17871790
// type parameter (e.g., `<T:Foo>`).
17881791
for param in &ast_generics.params {
1789-
match param.kind {
1790-
GenericParamKind::Type { .. } => {
1791-
let name = param.name.ident().as_interned_str();
1792-
let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
1793-
index += 1;
1794-
1795-
let sized = SizedByDefault::Yes;
1796-
let bounds = compute_bounds(&icx, param_ty, &param.bounds, sized, param.span);
1797-
predicates.extend(bounds.predicates(tcx, param_ty));
1798-
}
1799-
_ => {}
1792+
if let GenericParamKind::Type { .. } = param.kind {
1793+
let name = param.name.ident().as_interned_str();
1794+
let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
1795+
index += 1;
1796+
1797+
let sized = SizedByDefault::Yes;
1798+
let bounds = compute_bounds(&icx, param_ty, &param.bounds, sized, param.span);
1799+
predicates.extend(bounds.predicates(tcx, param_ty));
18001800
}
18011801
}
18021802

@@ -1835,10 +1835,10 @@ fn explicit_predicates_of<'a, 'tcx>(
18351835
&mut projections,
18361836
);
18371837

1838-
predicates.push((trait_ref.to_predicate(), poly_trait_ref.span));
1839-
predicates.extend(projections.iter().map(|&(p, span)| {
1840-
(p.to_predicate(), span)
1841-
}));
1838+
predicates.extend(
1839+
iter::once((trait_ref.to_predicate(), poly_trait_ref.span)).chain(
1840+
projections.iter().map(|&(p, span)| (p.to_predicate(), span)
1841+
)));
18421842
}
18431843

18441844
&hir::GenericBound::Outlives(ref lifetime) => {
@@ -1852,16 +1852,17 @@ fn explicit_predicates_of<'a, 'tcx>(
18521852

18531853
&hir::WherePredicate::RegionPredicate(ref region_pred) => {
18541854
let r1 = AstConv::ast_region_to_region(&icx, &region_pred.lifetime, None);
1855-
for bound in &region_pred.bounds {
1855+
predicates.extend(region_pred.bounds.iter().map(|bound| {
18561856
let (r2, span) = match bound {
18571857
hir::GenericBound::Outlives(lt) => {
18581858
(AstConv::ast_region_to_region(&icx, lt, None), lt.span)
18591859
}
18601860
_ => bug!(),
18611861
};
18621862
let pred = ty::Binder::bind(ty::OutlivesPredicate(r1, r2));
1863-
predicates.push((ty::Predicate::RegionOutlives(pred), span))
1864-
}
1863+
1864+
(ty::Predicate::RegionOutlives(pred), span)
1865+
}))
18651866
}
18661867

18671868
&hir::WherePredicate::EqPredicate(..) => {
@@ -1876,9 +1877,7 @@ fn explicit_predicates_of<'a, 'tcx>(
18761877
let trait_item = tcx.hir.trait_item(trait_item_ref.id);
18771878
let bounds = match trait_item.node {
18781879
hir::TraitItemKind::Type(ref bounds, _) => bounds,
1879-
_ => {
1880-
return vec![].into_iter();
1881-
}
1880+
_ => return vec![].into_iter()
18821881
};
18831882

18841883
let assoc_ty =
@@ -1939,6 +1938,7 @@ pub fn compute_bounds<'gcx: 'tcx, 'tcx>(
19391938
) -> Bounds<'tcx> {
19401939
let mut region_bounds = vec![];
19411940
let mut trait_bounds = vec![];
1941+
19421942
for ast_bound in ast_bounds {
19431943
match *ast_bound {
19441944
hir::GenericBound::Trait(ref b, hir::TraitBoundModifier::None) => trait_bounds.push(b),
@@ -2032,16 +2032,16 @@ fn compute_sig_of_foreign_fn_decl<'a, 'tcx>(
20322032
let check = |ast_ty: &hir::Ty, ty: Ty| {
20332033
if ty.is_simd() {
20342034
tcx.sess
2035-
.struct_span_err(
2036-
ast_ty.span,
2037-
&format!(
2038-
"use of SIMD type `{}` in FFI is highly experimental and \
2039-
may result in invalid code",
2040-
tcx.hir.node_to_pretty_string(ast_ty.id)
2041-
),
2042-
)
2043-
.help("add #![feature(simd_ffi)] to the crate attributes to enable")
2044-
.emit();
2035+
.struct_span_err(
2036+
ast_ty.span,
2037+
&format!(
2038+
"use of SIMD type `{}` in FFI is highly experimental and \
2039+
may result in invalid code",
2040+
tcx.hir.node_to_pretty_string(ast_ty.id)
2041+
),
2042+
)
2043+
.help("add #![feature(simd_ffi)] to the crate attributes to enable")
2044+
.emit();
20452045
}
20462046
};
20472047
for (input, ty) in decl.inputs.iter().zip(*fty.inputs().skip_binder()) {
@@ -2101,7 +2101,7 @@ fn from_target_feature(
21012101
};
21022102

21032103
// We allow comma separation to enable multiple features
2104-
for feature in value.as_str().split(',') {
2104+
target_features.extend(value.as_str().split(',').filter_map(|feature| {
21052105
// Only allow whitelisted features per platform
21062106
let feature_gate = match whitelist.get(feature) {
21072107
Some(g) => g,
@@ -2120,7 +2120,7 @@ fn from_target_feature(
21202120
}
21212121
}
21222122
err.emit();
2123-
continue;
2123+
return None;
21242124
}
21252125
};
21262126

@@ -2147,10 +2147,10 @@ fn from_target_feature(
21472147
feature_gate::GateIssue::Language,
21482148
&format!("the target feature `{}` is currently unstable", feature),
21492149
);
2150-
continue;
2150+
return None;
21512151
}
2152-
target_features.push(Symbol::intern(feature));
2153-
}
2152+
Some(Symbol::intern(feature))
2153+
}));
21542154
}
21552155
}
21562156

@@ -2183,7 +2183,7 @@ fn linkage_by_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, name: &
21832183
tcx.sess.span_fatal(span, "invalid linkage specified")
21842184
} else {
21852185
tcx.sess
2186-
.fatal(&format!("invalid linkage specified: {}", name))
2186+
.fatal(&format!("invalid linkage specified: {}", name))
21872187
}
21882188
}
21892189
}
@@ -2281,7 +2281,7 @@ fn codegen_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> Codegen
22812281
E0558,
22822282
"`export_name` attribute has invalid format"
22832283
).span_label(attr.span, "did you mean #[export_name=\"*\"]?")
2284-
.emit();
2284+
.emit();
22852285
}
22862286
} else if attr.check_name("target_feature") {
22872287
if tcx.fn_sig(id).unsafety() == Unsafety::Normal {

‎src/librustc_typeck/constrained_type_params.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ impl<'tcx> TypeVisitor<'tcx> for ParameterCollector {
7777
}
7878

7979
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
80-
match *r {
81-
ty::ReEarlyBound(data) => {
82-
self.parameters.push(Parameter::from(data));
83-
}
84-
_ => {}
80+
if let ty::ReEarlyBound(data) = *r {
81+
self.parameters.push(Parameter::from(data));
8582
}
8683
false
8784
}
@@ -204,6 +201,6 @@ pub fn setup_constraining_predicates<'tcx>(tcx: TyCtxt,
204201
}
205202
debug!("setup_constraining_predicates: predicates={:?} \
206203
i={} impl_trait_ref={:?} input_parameters={:?}",
207-
predicates, i, impl_trait_ref, input_parameters);
204+
predicates, i, impl_trait_ref, input_parameters);
208205
}
209206
}

‎src/librustc_typeck/impl_wf_check.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,12 @@ struct ImplWfCheck<'a, 'tcx: 'a> {
7171

7272
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
7373
fn visit_item(&mut self, item: &'tcx hir::Item) {
74-
match item.node {
75-
hir::ItemKind::Impl(.., ref impl_item_refs) => {
76-
let impl_def_id = self.tcx.hir.local_def_id(item.id);
77-
enforce_impl_params_are_constrained(self.tcx,
78-
impl_def_id,
79-
impl_item_refs);
80-
enforce_impl_items_are_distinct(self.tcx, impl_item_refs);
81-
}
82-
_ => { }
74+
if let hir::ItemKind::Impl(.., ref impl_item_refs) = item.node {
75+
let impl_def_id = self.tcx.hir.local_def_id(item.id);
76+
enforce_impl_params_are_constrained(self.tcx,
77+
impl_def_id,
78+
impl_item_refs);
79+
enforce_impl_items_are_distinct(self.tcx, impl_item_refs);
8380
}
8481
}
8582

@@ -182,7 +179,7 @@ fn enforce_impl_items_are_distinct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
182179
let impl_item = tcx.hir.impl_item(impl_item_ref.id);
183180
let seen_items = match impl_item.node {
184181
hir::ImplItemKind::Type(_) => &mut seen_type_items,
185-
_ => &mut seen_value_items,
182+
_ => &mut seen_value_items,
186183
};
187184
match seen_items.entry(impl_item.ident.modern()) {
188185
Occupied(entry) => {
@@ -191,7 +188,7 @@ fn enforce_impl_items_are_distinct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
191188
impl_item.ident);
192189
err.span_label(*entry.get(),
193190
format!("previous definition of `{}` here",
194-
impl_item.ident));
191+
impl_item.ident));
195192
err.span_label(impl_item.span, "duplicate definition");
196193
err.emit();
197194
}

‎src/librustc_typeck/lib.rs

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn require_c_abi_if_variadic(tcx: TyCtxt,
146146
span: Span) {
147147
if decl.variadic && !(abi == Abi::C || abi == Abi::Cdecl) {
148148
let mut err = struct_span_err!(tcx.sess, span, E0045,
149-
"variadic function must have C or cdecl calling convention");
149+
"variadic function must have C or cdecl calling convention");
150150
err.span_label(span, "variadics require C or cdecl calling convention").emit();
151151
}
152152
}
@@ -186,35 +186,29 @@ fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
186186
let main_t = tcx.type_of(main_def_id);
187187
match main_t.sty {
188188
ty::FnDef(..) => {
189-
match tcx.hir.find(main_id) {
190-
Some(Node::Item(it)) => {
191-
match it.node {
192-
hir::ItemKind::Fn(.., ref generics, _) => {
193-
let mut error = false;
194-
if !generics.params.is_empty() {
195-
let msg = "`main` function is not allowed to have generic \
196-
parameters".to_string();
197-
let label = "`main` cannot have generic parameters".to_string();
198-
struct_span_err!(tcx.sess, generics.span, E0131, "{}", msg)
199-
.span_label(generics.span, label)
200-
.emit();
201-
error = true;
202-
}
203-
if let Some(sp) = generics.where_clause.span() {
204-
struct_span_err!(tcx.sess, sp, E0646,
205-
"`main` function is not allowed to have a `where` clause")
206-
.span_label(sp, "`main` cannot have a `where` clause")
207-
.emit();
208-
error = true;
209-
}
210-
if error {
211-
return;
212-
}
213-
}
214-
_ => ()
189+
if let Some(Node::Item(it)) = tcx.hir.find(main_id) {
190+
if let hir::ItemKind::Fn(.., ref generics, _) = it.node {
191+
let mut error = false;
192+
if !generics.params.is_empty() {
193+
let msg = "`main` function is not allowed to have generic \
194+
parameters".to_owned();
195+
let label = "`main` cannot have generic parameters".to_string();
196+
struct_span_err!(tcx.sess, generics.span, E0131, "{}", msg)
197+
.span_label(generics.span, label)
198+
.emit();
199+
error = true;
200+
}
201+
if let Some(sp) = generics.where_clause.span() {
202+
struct_span_err!(tcx.sess, sp, E0646,
203+
"`main` function is not allowed to have a `where` clause")
204+
.span_label(sp, "`main` cannot have a `where` clause")
205+
.emit();
206+
error = true;
207+
}
208+
if error {
209+
return;
215210
}
216211
}
217-
_ => ()
218212
}
219213

220214
let actual = tcx.fn_sig(main_def_id);
@@ -258,34 +252,28 @@ fn check_start_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
258252
let start_t = tcx.type_of(start_def_id);
259253
match start_t.sty {
260254
ty::FnDef(..) => {
261-
match tcx.hir.find(start_id) {
262-
Some(Node::Item(it)) => {
263-
match it.node {
264-
hir::ItemKind::Fn(.., ref generics, _) => {
265-
let mut error = false;
266-
if !generics.params.is_empty() {
267-
struct_span_err!(tcx.sess, generics.span, E0132,
268-
"start function is not allowed to have type parameters")
269-
.span_label(generics.span,
270-
"start function cannot have type parameters")
271-
.emit();
272-
error = true;
273-
}
274-
if let Some(sp) = generics.where_clause.span() {
275-
struct_span_err!(tcx.sess, sp, E0647,
276-
"start function is not allowed to have a `where` clause")
277-
.span_label(sp, "start function cannot have a `where` clause")
278-
.emit();
279-
error = true;
280-
}
281-
if error {
282-
return;
283-
}
284-
}
285-
_ => ()
255+
if let Some(Node::Item(it)) = tcx.hir.find(start_id) {
256+
if let hir::ItemKind::Fn(.., ref generics, _) = it.node {
257+
let mut error = false;
258+
if !generics.params.is_empty() {
259+
struct_span_err!(tcx.sess, generics.span, E0132,
260+
"start function is not allowed to have type parameters")
261+
.span_label(generics.span,
262+
"start function cannot have type parameters")
263+
.emit();
264+
error = true;
265+
}
266+
if let Some(sp) = generics.where_clause.span() {
267+
struct_span_err!(tcx.sess, sp, E0647,
268+
"start function is not allowed to have a `where` clause")
269+
.span_label(sp, "start function cannot have a `where` clause")
270+
.emit();
271+
error = true;
272+
}
273+
if error {
274+
return;
286275
}
287276
}
288-
_ => ()
289277
}
290278

291279
let se_ty = tcx.mk_fn_ptr(ty::Binder::bind(
@@ -388,6 +376,7 @@ pub fn hir_ty_to_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, hir_ty: &hir::Ty) ->
388376
let env_node_id = tcx.hir.get_parent(hir_ty.id);
389377
let env_def_id = tcx.hir.local_def_id(env_node_id);
390378
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id);
379+
391380
astconv::AstConv::ast_ty_to_ty(&item_cx, hir_ty)
392381
}
393382

@@ -403,6 +392,7 @@ pub fn hir_trait_to_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, hir_trait:
403392
let principal = astconv::AstConv::instantiate_poly_trait_ref_inner(
404393
&item_cx, hir_trait, tcx.types.err, &mut projections, true
405394
);
395+
406396
(principal, projections)
407397
}
408398

‎src/librustc_typeck/outlives/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ fn inferred_outlives_of<'a, 'tcx>(
5555
.iter()
5656
.map(|out_pred| match out_pred {
5757
ty::Predicate::RegionOutlives(p) => p.to_string(),
58-
5958
ty::Predicate::TypeOutlives(p) => p.to_string(),
60-
6159
err => bug!("unexpected predicate {:?}", err),
6260
}).collect();
6361
pred.sort();

‎src/librustc_typeck/outlives/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc::ty::TyCtxt;
1414

1515
pub fn test_inferred_outlives<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
1616
tcx.hir
17-
.krate()
18-
.visit_all_item_likes(&mut OutlivesTest { tcx });
17+
.krate()
18+
.visit_all_item_likes(&mut OutlivesTest { tcx });
1919
}
2020

2121
struct OutlivesTest<'a, 'tcx: 'a> {

‎src/librustc_typeck/outlives/utils.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,9 @@ fn is_free_region<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, region: Region<'_>) -> bool
148148
// field: &'static T, // this would generate a ReStatic
149149
// }
150150
RegionKind::ReStatic => {
151-
if tcx
152-
.sess
153-
.features_untracked()
154-
.infer_static_outlives_requirements
155-
{
156-
true
157-
} else {
158-
false
159-
}
151+
tcx.sess
152+
.features_untracked()
153+
.infer_static_outlives_requirements
160154
}
161155

162156
// Late-bound regions can appear in `fn` types:

‎src/librustc_typeck/variance/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn crate_variances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
5555
}
5656

5757
fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
58-
-> Lrc<Vec<ty::Variance>> {
58+
-> Lrc<Vec<ty::Variance>> {
5959
let id = tcx.hir.as_local_node_id(item_def_id).expect("expected local def-id");
6060
let unsupported = || {
6161
// Variance not relevant.

0 commit comments

Comments
 (0)
Please sign in to comment.