Skip to content

Commit fd2766e

Browse files
committedSep 11, 2022
Check that the types in RPITITs are WF
1 parent 98f3001 commit fd2766e

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
 

‎compiler/rustc_typeck/src/check/wfcheck.rs

+44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::constrained_generic_params::{identify_constrained_generic_params, Parameter};
2+
use hir::def::DefKind;
23
use rustc_ast as ast;
34
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
45
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
@@ -1530,6 +1531,49 @@ fn check_fn_or_method<'tcx>(
15301531
);
15311532

15321533
check_where_clauses(wfcx, span, def_id);
1534+
1535+
check_return_position_impl_trait_in_trait_bounds(
1536+
tcx,
1537+
wfcx,
1538+
def_id,
1539+
sig.output(),
1540+
hir_decl.output.span(),
1541+
);
1542+
}
1543+
1544+
/// Basically `check_associated_type_bounds`, but separated for now and should be
1545+
/// deduplicated when RPITITs get lowered into real associated items.
1546+
fn check_return_position_impl_trait_in_trait_bounds<'tcx>(
1547+
tcx: TyCtxt<'tcx>,
1548+
wfcx: &WfCheckingCtxt<'_, 'tcx>,
1549+
fn_def_id: LocalDefId,
1550+
fn_output: Ty<'tcx>,
1551+
span: Span,
1552+
) {
1553+
if let Some(assoc_item) = tcx.opt_associated_item(fn_def_id.to_def_id())
1554+
&& assoc_item.container == ty::AssocItemContainer::TraitContainer
1555+
{
1556+
for arg in fn_output.walk() {
1557+
if let ty::GenericArgKind::Type(ty) = arg.unpack()
1558+
&& let ty::Projection(proj) = ty.kind()
1559+
&& tcx.def_kind(proj.item_def_id) == DefKind::ImplTraitPlaceholder
1560+
&& tcx.impl_trait_in_trait_parent(proj.item_def_id) == fn_def_id.to_def_id()
1561+
{
1562+
let bounds = wfcx.tcx().explicit_item_bounds(proj.item_def_id);
1563+
let wf_obligations = bounds.iter().flat_map(|&(bound, bound_span)| {
1564+
let normalized_bound = wfcx.normalize(span, None, bound);
1565+
traits::wf::predicate_obligations(
1566+
wfcx.infcx,
1567+
wfcx.param_env,
1568+
wfcx.body_id,
1569+
normalized_bound,
1570+
bound_span,
1571+
)
1572+
});
1573+
wfcx.register_obligations(wf_obligations);
1574+
}
1575+
}
1576+
}
15331577
}
15341578

15351579
const HELP_FOR_SELF_TYPE: &str = "consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// issue #101663
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
trait Wf<T> {}
7+
8+
trait Uwu {
9+
fn nya() -> impl Wf<Vec<[u8]>>;
10+
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
11+
12+
fn nya2() -> impl Wf<[u8]>;
13+
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2+
--> $DIR/wf-bounds.rs:9:22
3+
|
4+
LL | fn nya() -> impl Wf<Vec<[u8]>>;
5+
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[u8]`
8+
note: required by a bound in `Vec`
9+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
10+
|
11+
LL | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
12+
| ^ required by this bound in `Vec`
13+
14+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
15+
--> $DIR/wf-bounds.rs:12:23
16+
|
17+
LL | fn nya2() -> impl Wf<[u8]>;
18+
| ^^^^^^^^ doesn't have a size known at compile-time
19+
|
20+
= help: the trait `Sized` is not implemented for `[u8]`
21+
note: required by a bound in `Wf`
22+
--> $DIR/wf-bounds.rs:6:10
23+
|
24+
LL | trait Wf<T> {}
25+
| ^ required by this bound in `Wf`
26+
help: consider relaxing the implicit `Sized` restriction
27+
|
28+
LL | trait Wf<T: ?Sized> {}
29+
| ++++++++
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)
Please sign in to comment.