Skip to content

Commit

Permalink
Do not filter substs in remap_generic_params_to_declaration_params.
Browse files Browse the repository at this point in the history
The relevant filtering should have been performed by borrowck.
  • Loading branch information
cjgillot committed Jan 11, 2023
1 parent b22c152 commit b7bb8a5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
}

let definition_ty = instantiated_ty
.remap_generic_params_to_declaration_params(opaque_type_key, self.tcx, false, origin)
.remap_generic_params_to_declaration_params(opaque_type_key, self.tcx, false)
.ty;

if !check_opaque_type_parameter_valid(
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_typeck/src/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
opaque_type_key,
self.fcx.infcx.tcx,
true,
decl.origin,
);

self.typeck_results.concrete_opaque_types.insert(opaque_type_key.def_id, hidden_type);
Expand Down
27 changes: 1 addition & 26 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use crate::ty::util::Discr;
pub use adt::*;
pub use assoc::*;
pub use generics::*;
use hir::OpaqueTyOrigin;
use rustc_ast as ast;
use rustc_ast::node_id::NodeMap;
use rustc_attr as attr;
Expand Down Expand Up @@ -1316,7 +1315,6 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
tcx: TyCtxt<'tcx>,
// typeck errors have subpar spans for opaque types, so delay error reporting until borrowck.
ignore_errors: bool,
origin: OpaqueTyOrigin,
) -> Self {
let OpaqueTypeKey { def_id, substs } = opaque_type_key;

Expand All @@ -1332,30 +1330,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
// This zip may have several times the same lifetime in `substs` paired with a different
// lifetime from `id_substs`. Simply `collect`ing the iterator is the correct behaviour:
// it will pick the last one, which is the one we introduced in the impl-trait desugaring.
let map = substs.iter().zip(id_substs);

let map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>> = match origin {
// HACK: The HIR lowering for async fn does not generate
// any `+ Captures<'x>` bounds for the `impl Future<...>`, so all async fns with lifetimes
// would now fail to compile. We should probably just make hir lowering fill this in properly.
OpaqueTyOrigin::AsyncFn(_) => map.collect(),
OpaqueTyOrigin::FnReturn(_) | OpaqueTyOrigin::TyAlias => {
// Opaque types may only use regions that are bound. So for
// ```rust
// type Foo<'a, 'b, 'c> = impl Trait<'a> + 'b;
// ```
// we may not use `'c` in the hidden type.
let variances = tcx.variances_of(def_id);
debug!(?variances);

map.filter(|(_, v)| {
let ty::GenericArgKind::Lifetime(lt) = v.unpack() else { return true };
let ty::ReEarlyBound(ebr) = lt.kind() else { bug!() };
variances[ebr.index as usize] == ty::Variance::Invariant
})
.collect()
}
};
let map = substs.iter().zip(id_substs).collect();
debug!("map = {:#?}", map);

// Convert the type from the function into a type valid outside
Expand Down
39 changes: 39 additions & 0 deletions tests/ui/impl-trait/issues/issue-105826.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// check-pass

use std::io::Write;

struct A(Vec<u8>);

struct B<'a> {
one: &'a mut A,
two: &'a mut Vec<u8>,
three: Vec<u8>,
}

impl<'a> B<'a> {
fn one(&mut self) -> &mut impl Write {
&mut self.one.0
}
fn two(&mut self) -> &mut impl Write {
&mut *self.two
}
fn three(&mut self) -> &mut impl Write {
&mut self.three
}
}

struct C<'a>(B<'a>);

impl<'a> C<'a> {
fn one(&mut self) -> &mut impl Write {
self.0.one()
}
fn two(&mut self) -> &mut impl Write {
self.0.two()
}
fn three(&mut self) -> &mut impl Write {
self.0.three()
}
}

fn main() {}

0 comments on commit b7bb8a5

Please sign in to comment.