Skip to content

Commit 8020f97

Browse files
committed
Poison check_well_formed if method receivers are invalid to prevent typeck from running on it
1 parent 6d1fc53 commit 8020f97

4 files changed

+42
-17
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
9494
f: F,
9595
) -> Result<(), ErrorGuaranteed>
9696
where
97-
F: for<'a> FnOnce(&WfCheckingCtxt<'a, 'tcx>),
97+
F: for<'a> FnOnce(&WfCheckingCtxt<'a, 'tcx>) -> Result<(), ErrorGuaranteed>,
9898
{
9999
let param_env = tcx.param_env(body_def_id);
100100
let infcx = &tcx.infer_ctxt().build();
@@ -105,7 +105,7 @@ where
105105
if !tcx.features().trivial_bounds {
106106
wfcx.check_false_global_bounds()
107107
}
108-
f(&mut wfcx);
108+
f(&mut wfcx)?;
109109

110110
let assumed_wf_types = wfcx.ocx.assumed_wf_types_and_report_errors(param_env, body_def_id)?;
111111

@@ -875,6 +875,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
875875
ty,
876876
trait_def_id,
877877
);
878+
Ok(())
878879
})
879880
} else {
880881
let mut diag = match ty.kind() {
@@ -956,6 +957,8 @@ fn check_associated_item(
956957
ty::ImplContainer => tcx.type_of(item.container_id(tcx)).instantiate_identity(),
957958
};
958959

960+
let mut res = Ok(());
961+
959962
match item.kind {
960963
ty::AssocKind::Const => {
961964
let ty = tcx.type_of(item.def_id).instantiate_identity();
@@ -972,7 +975,7 @@ fn check_associated_item(
972975
hir_sig.decl,
973976
item.def_id.expect_local(),
974977
);
975-
check_method_receiver(wfcx, hir_sig, item, self_ty);
978+
res = res.and(check_method_receiver(wfcx, hir_sig, item, self_ty));
976979
}
977980
ty::AssocKind::Type => {
978981
if let ty::AssocItemContainer::TraitContainer = item.container {
@@ -985,6 +988,7 @@ fn check_associated_item(
985988
}
986989
}
987990
}
991+
res
988992
})
989993
}
990994

@@ -1097,6 +1101,7 @@ fn check_type_defn<'tcx>(
10971101
}
10981102

10991103
check_where_clauses(wfcx, item.span, item.owner_id.def_id);
1104+
Ok(())
11001105
})
11011106
}
11021107

@@ -1121,7 +1126,8 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) -> Result<(), ErrorGuarant
11211126
}
11221127

11231128
let res = enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| {
1124-
check_where_clauses(wfcx, item.span, def_id)
1129+
check_where_clauses(wfcx, item.span, def_id);
1130+
Ok(())
11251131
});
11261132

11271133
// Only check traits, don't check trait aliases
@@ -1164,6 +1170,7 @@ fn check_item_fn(
11641170
enter_wf_checking_ctxt(tcx, span, def_id, |wfcx| {
11651171
let sig = tcx.fn_sig(def_id).instantiate_identity();
11661172
check_fn_or_method(wfcx, ident.span, sig, decl, def_id);
1173+
Ok(())
11671174
})
11681175
}
11691176

@@ -1218,6 +1225,7 @@ fn check_item_type(
12181225
tcx.require_lang_item(LangItem::Sync, Some(ty_span)),
12191226
);
12201227
}
1228+
Ok(())
12211229
})
12221230
}
12231231

@@ -1276,6 +1284,7 @@ fn check_impl<'tcx>(
12761284
}
12771285

12781286
check_where_clauses(wfcx, item.span, item.owner_id.def_id);
1287+
Ok(())
12791288
})
12801289
}
12811290

@@ -1548,11 +1557,11 @@ fn check_method_receiver<'tcx>(
15481557
fn_sig: &hir::FnSig<'_>,
15491558
method: ty::AssocItem,
15501559
self_ty: Ty<'tcx>,
1551-
) {
1560+
) -> Result<(), ErrorGuaranteed> {
15521561
let tcx = wfcx.tcx();
15531562

15541563
if !method.fn_has_self_parameter {
1555-
return;
1564+
return Ok(());
15561565
}
15571566

15581567
let span = fn_sig.decl.inputs[0].span;
@@ -1571,11 +1580,11 @@ fn check_method_receiver<'tcx>(
15711580
if tcx.features().arbitrary_self_types {
15721581
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
15731582
// Report error; `arbitrary_self_types` was enabled.
1574-
e0307(tcx, span, receiver_ty);
1583+
return Err(e0307(tcx, span, receiver_ty));
15751584
}
15761585
} else {
15771586
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, false) {
1578-
if receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
1587+
return Err(if receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
15791588
// Report error; would have worked with `arbitrary_self_types`.
15801589
feature_err(
15811590
&tcx.sess.parse_sess,
@@ -1587,16 +1596,17 @@ fn check_method_receiver<'tcx>(
15871596
),
15881597
)
15891598
.help(HELP_FOR_SELF_TYPE)
1590-
.emit();
1599+
.emit()
15911600
} else {
15921601
// Report error; would not have worked with `arbitrary_self_types`.
1593-
e0307(tcx, span, receiver_ty);
1594-
}
1602+
e0307(tcx, span, receiver_ty)
1603+
});
15951604
}
15961605
}
1606+
Ok(())
15971607
}
15981608

1599-
fn e0307(tcx: TyCtxt<'_>, span: Span, receiver_ty: Ty<'_>) {
1609+
fn e0307(tcx: TyCtxt<'_>, span: Span, receiver_ty: Ty<'_>) -> ErrorGuaranteed {
16001610
struct_span_err!(
16011611
tcx.sess.diagnostic(),
16021612
span,
@@ -1605,7 +1615,7 @@ fn e0307(tcx: TyCtxt<'_>, span: Span, receiver_ty: Ty<'_>) {
16051615
)
16061616
.note("type of `self` must be `Self` or a type that dereferences to it")
16071617
.help(HELP_FOR_SELF_TYPE)
1608-
.emit();
1618+
.emit()
16091619
}
16101620

16111621
/// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: `R` cannot be used as the type of `self` without the `arbitrary_self_types` feature
2+
--> $DIR/arbitrary-self-from-method-substs.rs:8:43
3+
|
4+
LL | fn get<R: Deref<Target = Self>>(self: R) -> u32 {
5+
| ^
6+
|
7+
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
8+
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
9+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

tests/ui/self/arbitrary-self-from-method-substs.stderr tests/ui/self/arbitrary-self-from-method-substs.feature.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/arbitrary-self-from-method-substs.rs:14:5
2+
--> $DIR/arbitrary-self-from-method-substs.rs:16:5
33
|
44
LL | foo.get::<&Foo>();
55
| ^^^ expected `&Foo`, found `Foo`
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
#![feature(arbitrary_self_types)]
1+
// revisions: default feature
2+
#![cfg_attr(feature, feature(arbitrary_self_types))]
23

34
use std::ops::Deref;
45

56
struct Foo(u32);
67
impl Foo {
7-
fn get<R: Deref<Target=Self>>(self: R) -> u32 {
8+
fn get<R: Deref<Target = Self>>(self: R) -> u32 {
9+
//[default]~^ ERROR: `R` cannot be used as the type of `self`
810
self.0
911
}
1012
}
1113

1214
fn main() {
1315
let mut foo = Foo(1);
1416
foo.get::<&Foo>();
15-
//~^ ERROR mismatched types
17+
//[feature]~^ ERROR mismatched types
1618
}

0 commit comments

Comments
 (0)