Skip to content

Commit 8273567

Browse files
Rollup merge of #87107 - oli-obk:tait_double, r=nikomatsakis
Loop over all opaque types instead of looking at just the first one with the same DefId This exposed a bug in VecMap and is needed for #86410 anyway r? ``@spastorino`` cc ``@nikomatsakis``
2 parents 2119976 + 587e8fd commit 8273567

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

compiler/rustc_data_structures/src/vec_map.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ impl<K, V> IntoIterator for VecMap<K, V> {
127127
}
128128
}
129129

130-
impl<K, V> Extend<(K, V)> for VecMap<K, V> {
130+
impl<K: PartialEq, V> Extend<(K, V)> for VecMap<K, V> {
131131
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
132-
self.0.extend(iter);
132+
for (k, v) in iter {
133+
self.insert(k, v);
134+
}
133135
}
134136

135-
fn extend_one(&mut self, item: (K, V)) {
136-
self.0.extend_one(item);
137+
fn extend_one(&mut self, (k, v): (K, V)) {
138+
self.insert(k, v);
137139
}
138140

139141
fn extend_reserve(&mut self, additional: usize) {

compiler/rustc_typeck/src/collect/type_of.rs

+13-23
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,10 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
509509
}
510510
}
511511

512+
#[instrument(skip(tcx), level = "debug")]
512513
fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
513514
use rustc_hir::{Expr, ImplItem, Item, TraitItem};
514515

515-
debug!("find_opaque_ty_constraints({:?})", def_id);
516-
517516
struct ConstraintLocator<'tcx> {
518517
tcx: TyCtxt<'tcx>,
519518
def_id: DefId,
@@ -522,13 +521,11 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
522521
}
523522

524523
impl ConstraintLocator<'_> {
524+
#[instrument(skip(self), level = "debug")]
525525
fn check(&mut self, def_id: LocalDefId) {
526526
// Don't try to check items that cannot possibly constrain the type.
527527
if !self.tcx.has_typeck_results(def_id) {
528-
debug!(
529-
"find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`: no typeck results",
530-
self.def_id, def_id,
531-
);
528+
debug!("no constraint: no typeck results");
532529
return;
533530
}
534531
// Calling `mir_borrowck` can lead to cycle errors through
@@ -540,21 +537,19 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
540537
.get_by(|(key, _)| key.def_id == self.def_id)
541538
.is_none()
542539
{
543-
debug!(
544-
"find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`",
545-
self.def_id, def_id,
546-
);
540+
debug!("no constraints in typeck results");
547541
return;
548542
}
549543
// Use borrowck to get the type with unerased regions.
550544
let concrete_opaque_types = &self.tcx.mir_borrowck(def_id).concrete_opaque_types;
551-
if let Some((opaque_type_key, concrete_type)) =
552-
concrete_opaque_types.iter().find(|(key, _)| key.def_id == self.def_id)
553-
{
554-
debug!(
555-
"find_opaque_ty_constraints: found constraint for `{:?}` at `{:?}`: {:?}",
556-
self.def_id, def_id, concrete_type,
557-
);
545+
debug!(?concrete_opaque_types);
546+
for (opaque_type_key, concrete_type) in concrete_opaque_types {
547+
if opaque_type_key.def_id != self.def_id {
548+
// Ignore constraints for other opaque types.
549+
continue;
550+
}
551+
552+
debug!(?concrete_type, ?opaque_type_key.substs, "found constraint");
558553

559554
// FIXME(oli-obk): trace the actual span from inference to improve errors.
560555
let span = self.tcx.def_span(def_id);
@@ -603,7 +598,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
603598

604599
if let Some((prev_span, prev_ty)) = self.found {
605600
if *concrete_type != prev_ty {
606-
debug!("find_opaque_ty_constraints: span={:?}", span);
601+
debug!(?span);
607602
// Found different concrete types for the opaque type.
608603
let mut err = self.tcx.sess.struct_span_err(
609604
span,
@@ -619,11 +614,6 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
619614
} else {
620615
self.found = Some((span, concrete_type));
621616
}
622-
} else {
623-
debug!(
624-
"find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`",
625-
self.def_id, def_id,
626-
);
627617
}
628618
}
629619
}

src/test/ui/type-alias-impl-trait/issue-85113.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ trait Output<'a> {}
1212
impl<'a> Output<'a> for &'a str {}
1313

1414
fn cool_fn<'a>(arg: &'a str) -> OpaqueOutputImpl<'a> {
15+
//~^ ERROR: concrete type differs from previous defining opaque type use
1516
let out: OpaqueOutputImpl<'a> = arg;
1617
arg
1718
}

src/test/ui/type-alias-impl-trait/issue-85113.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ note: hidden type `&'<empty> str` captures lifetime smaller than the function bo
1010
LL | type OpaqueOutputImpl<'a> = impl Output<'a> + 'a;
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

13+
error: concrete type differs from previous defining opaque type use
14+
--> $DIR/issue-85113.rs:14:1
15+
|
16+
LL | fn cool_fn<'a>(arg: &'a str) -> OpaqueOutputImpl<'a> {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'<empty> str`, got `&'a str`
18+
|
19+
note: previous use here
20+
--> $DIR/issue-85113.rs:14:1
21+
|
22+
LL | fn cool_fn<'a>(arg: &'a str) -> OpaqueOutputImpl<'a> {
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
1325
error[E0477]: the type `&'<empty> str` does not fulfill the required lifetime
1426
--> $DIR/issue-85113.rs:5:29
1527
|
@@ -42,7 +54,7 @@ LL | type OpaqueOutputImpl<'a> = impl Output<'a> + 'a;
4254
= note: expected `Output<'a>`
4355
found `Output<'_>`
4456

45-
error: aborting due to 3 previous errors
57+
error: aborting due to 4 previous errors
4658

4759
Some errors have detailed explanations: E0477, E0495, E0700.
4860
For more information about an error, try `rustc --explain E0477`.

0 commit comments

Comments
 (0)