Skip to content

Commit a8ecb79

Browse files
authoredFeb 4, 2025
Rollup merge of #136274 - compiler-errors:sized-wf, r=lcnr
Check Sizedness of return type in WF Still need to clean this up a bit. This should fix rust-lang/trait-system-refactor-initiative#150. r? lcnr
2 parents 40c4e05 + 3683975 commit a8ecb79

File tree

61 files changed

+508
-428
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+508
-428
lines changed
 

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

+28-2
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ fn check_associated_item(
10651065
let ty = tcx.type_of(item.def_id).instantiate_identity();
10661066
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
10671067
wfcx.register_wf_obligation(span, loc, ty.into());
1068+
check_sized_if_body(wfcx, item.def_id.expect_local(), ty, Some(span));
10681069
Ok(())
10691070
}
10701071
ty::AssocKind::Fn => {
@@ -1189,7 +1190,7 @@ fn check_type_defn<'tcx>(
11891190
),
11901191
wfcx.param_env,
11911192
ty,
1192-
tcx.require_lang_item(LangItem::Sized, None),
1193+
tcx.require_lang_item(LangItem::Sized, Some(hir_ty.span)),
11931194
);
11941195
}
11951196

@@ -1314,7 +1315,7 @@ fn check_item_type(
13141315
),
13151316
wfcx.param_env,
13161317
item_ty,
1317-
tcx.require_lang_item(LangItem::Sized, None),
1318+
tcx.require_lang_item(LangItem::Sized, Some(ty_span)),
13181319
);
13191320
}
13201321

@@ -1644,6 +1645,31 @@ fn check_fn_or_method<'tcx>(
16441645
);
16451646
}
16461647
}
1648+
1649+
// If the function has a body, additionally require that the return type is sized.
1650+
check_sized_if_body(wfcx, def_id, sig.output(), match hir_decl.output {
1651+
hir::FnRetTy::Return(ty) => Some(ty.span),
1652+
hir::FnRetTy::DefaultReturn(_) => None,
1653+
});
1654+
}
1655+
1656+
fn check_sized_if_body<'tcx>(
1657+
wfcx: &WfCheckingCtxt<'_, 'tcx>,
1658+
def_id: LocalDefId,
1659+
ty: Ty<'tcx>,
1660+
maybe_span: Option<Span>,
1661+
) {
1662+
let tcx = wfcx.tcx();
1663+
if let Some(body) = tcx.hir().maybe_body_owned_by(def_id) {
1664+
let span = maybe_span.unwrap_or(body.value.span);
1665+
1666+
wfcx.register_bound(
1667+
ObligationCause::new(span, def_id, traits::ObligationCauseCode::SizedReturnType),
1668+
wfcx.param_env,
1669+
ty,
1670+
tcx.require_lang_item(LangItem::Sized, Some(span)),
1671+
);
1672+
}
16471673
}
16481674

16491675
/// The `arbitrary_self_types_pointers` feature implies `arbitrary_self_types`.

‎compiler/rustc_hir_typeck/src/check.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,17 @@ pub(super) fn check_fn<'a, 'tcx>(
117117

118118
fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
119119

120-
let return_or_body_span = match decl.output {
121-
hir::FnRetTy::DefaultReturn(_) => body.value.span,
122-
hir::FnRetTy::Return(ty) => ty.span,
123-
};
124-
125-
fcx.require_type_is_sized(
126-
declared_ret_ty,
127-
return_or_body_span,
128-
ObligationCauseCode::SizedReturnType,
129-
);
130-
// We checked the root's signature during wfcheck, but not the child.
120+
// We checked the root's ret ty during wfcheck, but not the child.
131121
if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
122+
let return_or_body_span = match decl.output {
123+
hir::FnRetTy::DefaultReturn(_) => body.value.span,
124+
hir::FnRetTy::Return(ty) => ty.span,
125+
};
126+
132127
fcx.require_type_is_sized(
133128
declared_ret_ty,
134129
return_or_body_span,
135-
ObligationCauseCode::WellFormed(None),
130+
ObligationCauseCode::SizedReturnType,
136131
);
137132
}
138133

0 commit comments

Comments
 (0)
Please sign in to comment.