Skip to content

Commit c8c3395

Browse files
committed
A few cleanups and minor improvements to typeck
1 parent beff387 commit c8c3395

15 files changed

+339
-381
lines changed

src/librustc_typeck/astconv.rs

+35-39
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 {
@@ -850,7 +850,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
850850
binding.span,
851851
E0582,
852852
"binding for associated type `{}` references lifetime `{}`, \
853-
which does not appear in the trait input types",
853+
which does not appear in the trait input types",
854854
binding.item_name, br_name)
855855
.emit();
856856
}
@@ -890,7 +890,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
890890
ref_id,
891891
binding.span,
892892
&format!("associated type binding `{}` specified more than once",
893-
binding.item_name)
893+
binding.item_name)
894894
);
895895
err.span_label(binding.span, "used more than once");
896896
err.span_label(*prev_span, format!("first use of `{}`", binding.item_name));
@@ -993,7 +993,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
993993
if !object_safety_violations.is_empty() {
994994
tcx.report_object_safety_error(
995995
span, principal.def_id(), object_safety_violations)
996-
.emit();
996+
.emit();
997997
return tcx.types.err;
998998
}
999999

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

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

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

10771076
}
@@ -1093,8 +1092,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
10931092

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

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

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

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

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

@@ -1292,16 +1289,15 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
12921289
if err_for_lt { continue }
12931290
err_for_lt = true;
12941291
(struct_span_err!(self.tcx().sess, lt.span, E0110,
1295-
"lifetime parameters are not allowed on \
1296-
this type"),
1292+
"lifetime parameters are not allowed on this type"),
12971293
lt.span,
12981294
"lifetime")
12991295
}
13001296
hir::GenericArg::Type(ty) => {
13011297
if err_for_ty { continue }
13021298
err_for_ty = true;
13031299
(struct_span_err!(self.tcx().sess, ty.span, E0109,
1304-
"type parameters are not allowed on this type"),
1300+
"type parameters are not allowed on this type"),
13051301
ty.span,
13061302
"type")
13071303
}
@@ -1589,7 +1585,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
15891585
));
15901586

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

src/librustc_typeck/check_unused.rs

+1-1
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

+33-41
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,29 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
5454
}
5555

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

@@ -118,11 +113,11 @@ fn visit_implementation_of_copy<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl_did:
118113
};
119114

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

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

182174
// this provider should only get invoked for local def-ids
183175
let impl_node_id = gcx.hir.as_local_node_id(impl_did).unwrap_or_else(|| {
@@ -209,9 +201,9 @@ pub fn coerce_unsized_info<'a, 'gcx>(gcx: TyCtxt<'a, 'gcx, 'gcx>,
209201
mk_ptr: &dyn Fn(Ty<'gcx>) -> Ty<'gcx>| {
210202
if (mt_a.mutbl, mt_b.mutbl) == (hir::MutImmutable, hir::MutMutable) {
211203
infcx.report_mismatched_types(&cause,
212-
mk_ptr(mt_b.ty),
213-
target,
214-
ty::error::TypeError::Mutability)
204+
mk_ptr(mt_b.ty),
205+
target,
206+
ty::error::TypeError::Mutability)
215207
.emit();
216208
}
217209
(mt_a.ty, mt_b.ty, unsize_trait, None)
@@ -234,7 +226,7 @@ pub fn coerce_unsized_info<'a, 'gcx>(gcx: TyCtxt<'a, 'gcx, 'gcx>,
234226
}
235227

236228
(&ty::Adt(def_a, substs_a), &ty::Adt(def_b, substs_b)) if def_a.is_struct() &&
237-
def_b.is_struct() => {
229+
def_b.is_struct() => {
238230
if def_a != def_b {
239231
let source_path = gcx.item_path_str(def_a.did);
240232
let target_path = gcx.item_path_str(def_b.did);

src/librustc_typeck/coherence/inherent_impls.rs

+1-2
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

+2-2
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).

0 commit comments

Comments
 (0)