Skip to content

Commit eca0ead

Browse files
committed
Enforce well formedness for type alias impl trait's hidden type
1 parent e50ff9b commit eca0ead

File tree

6 files changed

+111
-2
lines changed

6 files changed

+111
-2
lines changed

compiler/rustc_typeck/src/check/check.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ use rustc_hir::lang_items::LangItem;
1212
use rustc_hir::{def::Res, ItemKind, Node, PathSegment};
1313
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1414
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
15+
use rustc_infer::traits::Obligation;
1516
use rustc_middle::hir::nested_filter;
1617
use rustc_middle::ty::fold::TypeFoldable;
1718
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
1819
use rustc_middle::ty::subst::GenericArgKind;
1920
use rustc_middle::ty::util::{Discr, IntTypeExt};
20-
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
21+
use rustc_middle::ty::{self, ParamEnv, ToPredicate, Ty, TyCtxt};
2122
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
2223
use rustc_span::symbol::sym;
2324
use rustc_span::{self, MultiSpan, Span};
@@ -674,6 +675,13 @@ fn check_opaque_meets_bounds<'tcx>(
674675
}
675676
}
676677

678+
// Additionally require the hidden type to be well-formed with only the generics of the opaque type.
679+
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
680+
// hidden type is well formed even without those bounds.
681+
let predicate =
682+
ty::Binder::dummy(ty::PredicateKind::WellFormed(hidden_type.into())).to_predicate(tcx);
683+
inh.register_predicate(Obligation::new(misc_cause, param_env, predicate));
684+
677685
// Check that all obligations are satisfied by the implementation's
678686
// version.
679687
let errors = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);

compiler/rustc_typeck/src/check/regionck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
171171

172172
/// Region checking during the WF phase for items. `wf_tys` are the
173173
/// types from which we should derive implied bounds, if any.
174+
#[instrument(level = "debug", skip(self))]
174175
pub fn regionck_item(&self, item_id: hir::HirId, span: Span, wf_tys: FxHashSet<Ty<'tcx>>) {
175-
debug!("regionck_item(item.id={:?}, wf_tys={:?})", item_id, wf_tys);
176176
let subject = self.tcx.hir().local_def_id(item_id);
177177
let mut rcx = RegionCtxt::new(self, item_id, Subject(subject), self.param_env);
178178
rcx.outlives_environment.add_implied_bounds(self, wf_tys, item_id, span);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
use std::marker::PhantomData;
4+
5+
trait Trait {
6+
fn foo<T, U>(t: T) -> U;
7+
}
8+
9+
trait ProofForConversion<X> {
10+
fn convert<T, U>(_: PhantomData<Self>, r: T) -> U;
11+
}
12+
13+
impl<X: Trait> ProofForConversion<X> for () {
14+
fn convert<T, U>(_: PhantomData<Self>, r: T) -> U {
15+
X::foo(r)
16+
}
17+
}
18+
19+
type Converter<T> = impl ProofForConversion<T>;
20+
//~^ ERROR the trait bound `T: Trait` is not satisfied
21+
22+
fn _defining_use<T: Trait>() -> Converter<T> {
23+
()
24+
}
25+
26+
27+
fn main() {
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0277]: the trait bound `T: Trait` is not satisfied
2+
--> $DIR/underconstrained_generic.rs:19:21
3+
|
4+
LL | type Converter<T> = impl ProofForConversion<T>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `T`
6+
|
7+
note: required because of the requirements on the impl of `ProofForConversion<T>` for `()`
8+
--> $DIR/underconstrained_generic.rs:13:16
9+
|
10+
LL | impl<X: Trait> ProofForConversion<X> for () {
11+
| ^^^^^^^^^^^^^^^^^^^^^ ^^
12+
help: consider restricting type parameter `T`
13+
|
14+
LL | type Converter<T: Trait> = impl ProofForConversion<T>;
15+
| +++++++
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
use std::marker::PhantomData;
4+
5+
trait ProofForConversion<'a, 'b> {
6+
fn convert<T: ?Sized>(_: PhantomData<Self>, r: &'a T) -> &'b T;
7+
}
8+
9+
impl<'a, 'b> ProofForConversion<'a, 'b> for &'b &'a () {
10+
fn convert<T: ?Sized>(_: PhantomData<Self>, r: &'a T) -> &'b T {
11+
r
12+
}
13+
}
14+
15+
type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>;
16+
//~^ ERROR reference has a longer lifetime than the data it references
17+
18+
// Even _defining_use with an explicit `'a: 'b` compiles fine, too.
19+
fn _defining_use<'a, 'b>(x: &'b &'a ()) -> Converter<'a, 'b> {
20+
x
21+
}
22+
23+
fn extend_lifetime<'a, 'b, T: ?Sized>(x: &'a T) -> &'b T {
24+
Converter::<'a, 'b>::convert(PhantomData, x)
25+
}
26+
27+
fn main() {
28+
let d;
29+
{
30+
let x = "Hello World".to_string();
31+
d = extend_lifetime(&x);
32+
}
33+
println!("{}", d);
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0491]: in type `&'b &'a ()`, reference has a longer lifetime than the data it references
2+
--> $DIR/underconstrained_lifetime.rs:15:26
3+
|
4+
LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the pointer is valid for the lifetime `'b` as defined here
8+
--> $DIR/underconstrained_lifetime.rs:15:20
9+
|
10+
LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>;
11+
| ^^
12+
note: but the referenced data is only valid for the lifetime `'a` as defined here
13+
--> $DIR/underconstrained_lifetime.rs:15:16
14+
|
15+
LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>;
16+
| ^^
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0491`.

0 commit comments

Comments
 (0)