Skip to content

Commit 8a84dd8

Browse files
authored
Rollup merge of #104890 - lcnr:small-cleanup, r=fee1-dead
small method code cleanup
2 parents 79fe15c + 99c3dda commit 8a84dd8

File tree

6 files changed

+47
-92
lines changed

6 files changed

+47
-92
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
179179

180180
// Hack: we know that there are traits implementing Fn for &F
181181
// where F:Fn and so forth. In the particular case of types
182-
// like `x: &mut FnMut()`, if there is a call `x()`, we would
183-
// normally translate to `FnMut::call_mut(&mut x, ())`, but
184-
// that winds up requiring `mut x: &mut FnMut()`. A little
185-
// over the top. The simplest fix by far is to just ignore
186-
// this case and deref again, so we wind up with
187-
// `FnMut::call_mut(&mut *x, ())`.
182+
// like `f: &mut FnMut()`, if there is a call `f()`, we would
183+
// normally translate to `FnMut::call_mut(&mut f, ())`, but
184+
// that winds up potentially requiring the user to mark their
185+
// variable as `mut` which feels unnecessary and unexpected.
186+
//
187+
// fn foo(f: &mut impl FnMut()) { f() }
188+
// ^ without this hack `f` would have to be declared as mutable
189+
//
190+
// The simplest fix by far is to just ignore this case and deref again,
191+
// so we wind up with `FnMut::call_mut(&mut *f, ())`.
188192
ty::Ref(..) if autoderef.step_count() == 0 => {
189193
return None;
190194
}

compiler/rustc_hir_typeck/src/cast.rs

-12
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use rustc_span::def_id::{DefId, LOCAL_CRATE};
4646
use rustc_span::symbol::sym;
4747
use rustc_span::Span;
4848
use rustc_trait_selection::infer::InferCtxtExt;
49-
use rustc_trait_selection::traits::error_reporting::report_object_safety_error;
5049

5150
/// Reifies a cast check to be checked once we have full type information for
5251
/// a function context.
@@ -727,9 +726,6 @@ impl<'a, 'tcx> CastCheck<'tcx> {
727726
debug!(" -> CoercionCast");
728727
fcx.typeck_results.borrow_mut().set_coercion_cast(self.expr.hir_id.local_id);
729728
}
730-
Err(ty::error::TypeError::ObjectUnsafeCoercion(did)) => {
731-
self.report_object_unsafe_cast(&fcx, did);
732-
}
733729
Err(_) => {
734730
match self.do_check(fcx) {
735731
Ok(k) => {
@@ -741,14 +737,6 @@ impl<'a, 'tcx> CastCheck<'tcx> {
741737
};
742738
}
743739
}
744-
745-
fn report_object_unsafe_cast(&self, fcx: &FnCtxt<'a, 'tcx>, did: DefId) {
746-
let violations = fcx.tcx.object_safety_violations(did);
747-
let mut err = report_object_safety_error(fcx.tcx, self.cast_span, did, violations);
748-
err.note(&format!("required by cast to type '{}'", fcx.ty_to_string(self.cast_ty)));
749-
err.emit();
750-
}
751-
752740
/// Checks a cast, and report an error if one exists. In some cases, this
753741
/// can return Ok and create type errors in the fcx rather than returning
754742
/// directly. coercion-cast is handled in check instead of here.

compiler/rustc_hir_typeck/src/coercion.rs

+6-29
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,6 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
195195
debug!("coerce: unsize successful");
196196
return unsize;
197197
}
198-
Err(TypeError::ObjectUnsafeCoercion(did)) => {
199-
debug!("coerce: unsize not object safe");
200-
return Err(TypeError::ObjectUnsafeCoercion(did));
201-
}
202198
Err(error) => {
203199
debug!(?error, "coerce: unsize failed");
204200
}
@@ -498,27 +494,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
498494
target = self.shallow_resolve(target);
499495
debug!(?source, ?target);
500496

501-
// These 'if' statements require some explanation.
502-
// The `CoerceUnsized` trait is special - it is only
503-
// possible to write `impl CoerceUnsized<B> for A` where
504-
// A and B have 'matching' fields. This rules out the following
505-
// two types of blanket impls:
506-
//
507-
// `impl<T> CoerceUnsized<T> for SomeType`
508-
// `impl<T> CoerceUnsized<SomeType> for T`
509-
//
510-
// Both of these trigger a special `CoerceUnsized`-related error (E0376)
511-
//
512-
// We can take advantage of this fact to avoid performing unnecessary work.
513-
// If either `source` or `target` is a type variable, then any applicable impl
514-
// would need to be generic over the self-type (`impl<T> CoerceUnsized<SomeType> for T`)
515-
// or generic over the `CoerceUnsized` type parameter (`impl<T> CoerceUnsized<T> for
516-
// SomeType`).
517-
//
518-
// However, these are exactly the kinds of impls which are forbidden by
519-
// the compiler! Therefore, we can be sure that coercion will always fail
520-
// when either the source or target type is a type variable. This allows us
521-
// to skip performing any trait selection, and immediately bail out.
497+
// We don't apply any coercions incase either the source or target
498+
// aren't sufficiently well known but tend to instead just equate
499+
// them both.
522500
if source.is_ty_var() {
523501
debug!("coerce_unsized: source is a TyVar, bailing out");
524502
return Err(TypeError::Mismatch);
@@ -1101,15 +1079,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11011079
// Special-case that coercion alone cannot handle:
11021080
// Function items or non-capturing closures of differing IDs or InternalSubsts.
11031081
let (a_sig, b_sig) = {
1104-
#[allow(rustc::usage_of_ty_tykind)]
1105-
let is_capturing_closure = |ty: &ty::TyKind<'tcx>| {
1106-
if let &ty::Closure(closure_def_id, _substs) = ty {
1082+
let is_capturing_closure = |ty: Ty<'tcx>| {
1083+
if let &ty::Closure(closure_def_id, _substs) = ty.kind() {
11071084
self.tcx.upvars_mentioned(closure_def_id.expect_local()).is_some()
11081085
} else {
11091086
false
11101087
}
11111088
};
1112-
if is_capturing_closure(prev_ty.kind()) || is_capturing_closure(new_ty.kind()) {
1089+
if is_capturing_closure(prev_ty) || is_capturing_closure(new_ty) {
11131090
(None, None)
11141091
} else {
11151092
match (prev_ty.kind(), new_ty.kind()) {

compiler/rustc_hir_typeck/src/method/probe.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
343343
&mut orig_values,
344344
);
345345

346-
let steps = if mode == Mode::MethodCall {
347-
self.tcx.method_autoderef_steps(param_env_and_self_ty)
348-
} else {
349-
self.probe(|_| {
346+
let steps = match mode {
347+
Mode::MethodCall => self.tcx.method_autoderef_steps(param_env_and_self_ty),
348+
Mode::Path => self.probe(|_| {
350349
// Mode::Path - the deref steps is "trivial". This turns
351350
// our CanonicalQuery into a "trivial" QueryResponse. This
352351
// is a bit inefficient, but I don't think that writing
@@ -375,7 +374,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
375374
opt_bad_ty: None,
376375
reached_recursion_limit: false,
377376
}
378-
})
377+
}),
379378
};
380379

381380
// If our autoderef loop had reached the recursion limit,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+26-36
Original file line numberDiff line numberDiff line change
@@ -1672,40 +1672,34 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
16721672
}
16731673
};
16741674

1675-
match terr {
1676-
// Ignore msg for object safe coercion
1677-
// since E0038 message will be printed
1678-
TypeError::ObjectUnsafeCoercion(_) => {}
1679-
_ => {
1680-
let mut label_or_note = |span: Span, msg: &str| {
1681-
if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() {
1682-
diag.span_label(span, msg);
1683-
} else {
1684-
diag.span_note(span, msg);
1685-
}
1686-
};
1687-
if let Some((sp, msg)) = secondary_span {
1688-
if swap_secondary_and_primary {
1689-
let terr = if let Some(infer::ValuePairs::Terms(infer::ExpectedFound {
1690-
expected,
1691-
..
1692-
})) = values
1693-
{
1694-
format!("expected this to be `{}`", expected)
1695-
} else {
1696-
terr.to_string()
1697-
};
1698-
label_or_note(sp, &terr);
1699-
label_or_note(span, &msg);
1700-
} else {
1701-
label_or_note(span, &terr.to_string());
1702-
label_or_note(sp, &msg);
1703-
}
1704-
} else {
1705-
label_or_note(span, &terr.to_string());
1706-
}
1675+
let mut label_or_note = |span: Span, msg: &str| {
1676+
if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() {
1677+
diag.span_label(span, msg);
1678+
} else {
1679+
diag.span_note(span, msg);
17071680
}
17081681
};
1682+
if let Some((sp, msg)) = secondary_span {
1683+
if swap_secondary_and_primary {
1684+
let terr = if let Some(infer::ValuePairs::Terms(infer::ExpectedFound {
1685+
expected,
1686+
..
1687+
})) = values
1688+
{
1689+
format!("expected this to be `{}`", expected)
1690+
} else {
1691+
terr.to_string()
1692+
};
1693+
label_or_note(sp, &terr);
1694+
label_or_note(span, &msg);
1695+
} else {
1696+
label_or_note(span, &terr.to_string());
1697+
label_or_note(sp, &msg);
1698+
}
1699+
} else {
1700+
label_or_note(span, &terr.to_string());
1701+
}
1702+
17091703
if let Some((expected, found)) = expected_found {
17101704
let (expected_label, found_label, exp_found) = match exp_found {
17111705
Mismatch::Variable(ef) => (
@@ -1875,9 +1869,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
18751869
);
18761870
}
18771871
}
1878-
TypeError::ObjectUnsafeCoercion(_) => {
1879-
diag.note_unsuccessful_coercion(found, expected);
1880-
}
18811872
_ => {
18821873
debug!(
18831874
"note_type_err: exp_found={:?}, expected={:?} found={:?}",
@@ -3122,7 +3113,6 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
31223113
TypeError::IntrinsicCast => {
31233114
Error0308("cannot coerce intrinsics to function pointers")
31243115
}
3125-
TypeError::ObjectUnsafeCoercion(did) => Error0038(did),
31263116
_ => Error0308("mismatched types"),
31273117
},
31283118
}

compiler/rustc_middle/src/ty/error.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub enum TypeError<'tcx> {
7070
CyclicConst(ty::Const<'tcx>),
7171
ProjectionMismatched(ExpectedFound<DefId>),
7272
ExistentialMismatch(ExpectedFound<&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>>),
73-
ObjectUnsafeCoercion(DefId),
7473
ConstMismatch(ExpectedFound<ty::Const<'tcx>>),
7574

7675
IntrinsicCast,
@@ -222,7 +221,6 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
222221
f,
223222
"cannot coerce functions with `#[target_feature]` to safe function pointers"
224223
),
225-
ObjectUnsafeCoercion(_) => write!(f, "coercion to object-unsafe trait object"),
226224
}
227225
}
228226
}
@@ -249,8 +247,7 @@ impl<'tcx> TypeError<'tcx> {
249247
| ProjectionMismatched(_)
250248
| ExistentialMismatch(_)
251249
| ConstMismatch(_)
252-
| IntrinsicCast
253-
| ObjectUnsafeCoercion(_) => true,
250+
| IntrinsicCast => true,
254251
}
255252
}
256253
}

0 commit comments

Comments
 (0)