Skip to content

Commit 9f19b50

Browse files
committed
better tip for return impl ?Sized
1 parent 99c4758 commit 9f19b50

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

compiler/rustc_typeck/src/check/check.rs

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::compare_method::check_type_bounds;
33
use super::compare_method::{compare_const_impl, compare_impl_method, compare_ty_impl};
44
use super::*;
55

6+
use hir::{GenericBound, PolyTraitRef, TraitBoundModifier};
67
use rustc_attr as attr;
78
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
89
use rustc_hir as hir;
@@ -96,6 +97,25 @@ pub(super) fn check_fn<'a, 'tcx>(
9697

9798
let declared_ret_ty = fn_sig.output();
9899

100+
if let ty::Opaque(def_id, _) = *declared_ret_ty.kind() {
101+
let bounds = match &tcx.hir().expect_item(def_id.expect_local()).kind {
102+
ItemKind::OpaqueTy(ty) => ty.bounds,
103+
_ => bug!("unexpected opaque type"),
104+
};
105+
106+
let sized_def_id = tcx.require_lang_item(LangItem::Sized, None);
107+
if bounds.iter().any(|bound| match bound {
108+
GenericBound::Trait(PolyTraitRef { trait_ref, .. }, TraitBoundModifier::Maybe)
109+
if trait_ref.path.res.def_id() == sized_def_id =>
110+
{
111+
true
112+
}
113+
_ => false,
114+
}) {
115+
sess.span_err(decl.output.span(), "return type should be sized");
116+
}
117+
}
118+
99119
let ret_ty =
100120
fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars(
101121
declared_ret_ty,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Regression test for issue #97226
2+
3+
fn test_fn() -> impl ?Sized {} //~ ERROR return type should be sized
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: return type should be sized
2+
--> $DIR/return-impl-maybe-sized.rs:3:17
3+
|
4+
LL | fn test_fn() -> impl ?Sized {}
5+
| ^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)