Skip to content

Commit e1be0d2

Browse files
committed
MIR typeck: invariant ctxt fast path
This removes variance information from some diagnostics. However, that variance information is not actually relevant here. Casting `*const dyn Cat<'a>` to `*const S<dyn Cat<'static>>` is an error regardless of whether `S` requires its argument to be invariant. Wide-pointer casts always require the trait object arguments to be invariant.
1 parent 1b71459 commit e1be0d2

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_middle::mir::ConstraintCategory;
1111
use rustc_middle::traits::ObligationCause;
1212
use rustc_middle::traits::query::NoSolution;
1313
use rustc_middle::ty::relate::combine::{combine_ty_args, super_combine_consts, super_combine_tys};
14+
use rustc_middle::ty::relate::relate_args_invariantly;
1415
use rustc_middle::ty::{self, FnMutDelegate, Ty, TyCtxt, TypeVisitableExt};
1516
use rustc_middle::{bug, span_bug};
1617
use rustc_span::{Span, Symbol, sym};
@@ -313,17 +314,24 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
313314
b_args: ty::GenericArgsRef<'tcx>,
314315
_: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
315316
) -> RelateResult<'tcx, Ty<'tcx>> {
316-
let variances = self.cx().variances_of(def_id);
317-
combine_ty_args(
318-
&self.type_checker.infcx.infcx,
319-
self,
320-
a_ty,
321-
b_ty,
322-
variances,
323-
a_args,
324-
b_args,
325-
|_| a_ty,
326-
)
317+
if self.ambient_variance == ty::Invariant {
318+
// Avoid fetching the variance if we are in an invariant context,
319+
// slightly improves perf.
320+
relate_args_invariantly(self, a_args, b_args)?;
321+
Ok(a_ty)
322+
} else {
323+
let variances = self.cx().variances_of(def_id);
324+
combine_ty_args(
325+
&self.type_checker.infcx.infcx,
326+
self,
327+
a_ty,
328+
b_ty,
329+
variances,
330+
a_args,
331+
b_args,
332+
|_| a_ty,
333+
)
334+
}
327335
}
328336

329337
#[instrument(skip(self, info), level = "trace", ret)]

tests/ui/cast/ptr-to-trait-obj-different-regions-id-trait.current.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ LL | fn m<'a>() {
66
LL | let unsend: *const dyn Cat<'a> = &();
77
LL | let _send = unsend as *const S<dyn Cat<'static>>;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
9-
|
10-
= note: requirement occurs because of the type `S<dyn Cat<'_>>`, which makes the generic argument `dyn Cat<'_>` invariant
11-
= note: the struct `S<T>` is invariant over the parameter `T`
12-
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
139

1410
error: aborting due to 1 previous error
1511

tests/ui/cast/ptr-to-trait-obj-different-regions-id-trait.next.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ LL | fn m<'a>() {
66
LL | let unsend: *const dyn Cat<'a> = &();
77
LL | let _send = unsend as *const S<dyn Cat<'static>>;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
9-
|
10-
= note: requirement occurs because of the type `S<dyn Cat<'_>>`, which makes the generic argument `dyn Cat<'_>` invariant
11-
= note: the struct `S<T>` is invariant over the parameter `T`
12-
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
139

1410
error: aborting due to 1 previous error
1511

tests/ui/fn/implied-bounds-unnorm-associated-type-2.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ LL | f::<'a, 'b>(());
99
| ^^^^^^^^^^^^^^^ generic argument requires that `'b` must outlive `'a`
1010
|
1111
= help: consider adding the following bound: `'b: 'a`
12-
= note: requirement occurs because of a function pointer to `f`
13-
= note: the function `f` is invariant over the parameter `'a`
14-
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
1512

1613
error: aborting due to 1 previous error
1714

0 commit comments

Comments
 (0)