Skip to content

Commit e0a1ab6

Browse files
committed
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
2 parents bb0896a + 608c395 commit e0a1ab6

15 files changed

+344
-392
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 {
@@ -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

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

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