Skip to content

Commit a9ce33c

Browse files
committed
Account for arbitrary self types in E0599
1 parent fba38ac commit a9ce33c

11 files changed

+144
-26
lines changed

src/librustc/ty/context.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,24 @@ impl<'tcx> TyCtxt<'tcx> {
24022402
self.mk_generic_adt(def_id, ty)
24032403
}
24042404

2405+
#[inline]
2406+
pub fn mk_pin(self, ty: Ty<'tcx>) -> Ty<'tcx> {
2407+
let def_id = self.require_lang_item(lang_items::PinTypeLangItem, None);
2408+
self.mk_generic_adt(def_id, ty)
2409+
}
2410+
2411+
#[inline]
2412+
pub fn mk_rc(self, ty: Ty<'tcx>) -> Ty<'tcx> {
2413+
let def_id = self.require_lang_item(lang_items::Rc, None);
2414+
self.mk_generic_adt(def_id, ty)
2415+
}
2416+
2417+
#[inline]
2418+
pub fn mk_arc(self, ty: Ty<'tcx>) -> Ty<'tcx> {
2419+
let def_id = self.require_lang_item(lang_items::Arc, None);
2420+
self.mk_generic_adt(def_id, ty)
2421+
}
2422+
24052423
#[inline]
24062424
pub fn mk_maybe_uninit(self, ty: Ty<'tcx>) -> Ty<'tcx> {
24072425
let def_id = self.require_lang_item(lang_items::MaybeUninitLangItem, None);

src/librustc_typeck/check/expr.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::check::fatally_break_rust;
1212
use crate::check::report_unexpected_variant_res;
1313
use crate::check::Needs;
1414
use crate::check::TupleArgumentsFlag::DontTupleArguments;
15-
use crate::check::method::SelfSource;
15+
use crate::check::method::{probe, SelfSource};
1616
use crate::util::common::ErrorReported;
1717
use crate::util::nodemap::FxHashMap;
1818
use crate::astconv::AstConv as _;
@@ -775,35 +775,65 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
775775
// no need to check for bot/err -- callee does that
776776
let rcvr_t = self.structurally_resolved_type(args[0].span, rcvr_t);
777777

778-
let method = match self.lookup_method(rcvr_t,
779-
segment,
780-
span,
781-
expr,
782-
rcvr) {
778+
let try_alt_rcvr = |err: &mut DiagnosticBuilder<'_>, new_rcvr_t| {
779+
if let Ok(pick) = self.lookup_probe(
780+
span,
781+
segment.ident,
782+
new_rcvr_t,
783+
rcvr,
784+
probe::ProbeScope::AllTraits,
785+
) {
786+
err.span_label(
787+
pick.item.ident.span,
788+
&format!("the method is available for `{}` here", new_rcvr_t),
789+
);
790+
}
791+
};
792+
793+
let method = match self.lookup_method(rcvr_t, segment, span, expr, rcvr) {
783794
Ok(method) => {
784795
self.write_method_call(expr.hir_id, method);
785796
Ok(method)
786797
}
787798
Err(error) => {
788799
if segment.ident.name != kw::Invalid {
789-
self.report_method_error(span,
790-
rcvr_t,
791-
segment.ident,
792-
SelfSource::MethodCall(rcvr),
793-
error,
794-
Some(args));
800+
if let Some(mut err) = self.report_method_error(
801+
span,
802+
rcvr_t,
803+
segment.ident,
804+
SelfSource::MethodCall(rcvr),
805+
error,
806+
Some(args),
807+
) {
808+
if let ty::Adt(..) = rcvr_t.sty {
809+
// Try alternative arbitrary self types that could fulfill this call.
810+
// FIXME: probe for all types that *could* be arbitrary self-types, not
811+
// just this whitelist.
812+
let box_rcvr_t = self.tcx.mk_box(rcvr_t);
813+
try_alt_rcvr(&mut err, box_rcvr_t);
814+
let pin_rcvr_t = self.tcx.mk_pin(rcvr_t);
815+
try_alt_rcvr(&mut err, pin_rcvr_t);
816+
let arc_rcvr_t = self.tcx.mk_arc(rcvr_t);
817+
try_alt_rcvr(&mut err, arc_rcvr_t);
818+
let rc_rcvr_t = self.tcx.mk_rc(rcvr_t);
819+
try_alt_rcvr(&mut err, rc_rcvr_t);
820+
}
821+
err.emit();
822+
}
795823
}
796824
Err(())
797825
}
798826
};
799827

800828
// Call the generic checker.
801-
self.check_method_argument_types(span,
802-
expr.span,
803-
method,
804-
&args[1..],
805-
DontTupleArguments,
806-
expected)
829+
self.check_method_argument_types(
830+
span,
831+
expr.span,
832+
method,
833+
&args[1..],
834+
DontTupleArguments,
835+
expected,
836+
)
807837
}
808838

809839
fn check_expr_cast(
@@ -1466,8 +1496,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14661496
let struct_variant_def = def.non_enum_variant();
14671497
let field_names = self.available_field_names(struct_variant_def);
14681498
if !field_names.is_empty() {
1469-
err.note(&format!("available fields are: {}",
1470-
self.name_series_display(field_names)));
1499+
err.note(&format!(
1500+
"available fields are: {}",
1501+
self.name_series_display(field_names),
1502+
));
14711503
}
14721504
}
14731505
}

src/librustc_typeck/check/method/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
245245
Ok(result.callee)
246246
}
247247

248-
fn lookup_probe(
248+
pub fn lookup_probe(
249249
&self,
250250
span: Span,
251251
method_name: ast::Ident,

src/librustc_typeck/check/method/suggest.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6969
source: SelfSource<'b>,
7070
error: MethodError<'tcx>,
7171
args: Option<&'tcx [hir::Expr]>,
72-
) {
72+
) -> Option<DiagnosticBuilder<'_>> {
7373
let orig_span = span;
7474
let mut span = span;
7575
// Avoid suggestions when we don't know what's going on.
7676
if rcvr_ty.references_error() {
77-
return;
77+
return None;
7878
}
7979

8080
let print_disambiguation_help = |
@@ -314,7 +314,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
314314
_ => {}
315315
}
316316
err.emit();
317-
return;
317+
return None;
318318
} else {
319319
span = item_name.span;
320320
let mut err = struct_span_err!(
@@ -529,7 +529,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
529529
);
530530
}
531531

532-
err.emit();
532+
return Some(err);
533533
}
534534

535535
MethodError::Ambiguity(sources) => {
@@ -573,6 +573,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
573573
bug!("no return type expectations but got BadReturnType")
574574
}
575575
}
576+
None
576577
}
577578

578579
fn suggest_use_candidates(&self,

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3580,7 +3580,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35803580
SelfSource::QPath(qself),
35813581
error,
35823582
None,
3583-
);
3583+
).map(|mut e| e.emit());
35843584
}
35853585
result
35863586
});

src/test/ui/impl-trait/no-method-suggested-traits.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ LL | use foo::Bar;
4949
error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::boxed::Box<&char>>` in the current scope
5050
--> $DIR/no-method-suggested-traits.rs:32:43
5151
|
52+
LL | fn method(&self) {}
53+
| ------
54+
| |
55+
| the method is available for `std::boxed::Box<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
56+
| the method is available for `std::pin::Pin<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
57+
| the method is available for `std::sync::Arc<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
58+
| the method is available for `std::rc::Rc<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
59+
...
5260
LL | std::rc::Rc::new(&mut Box::new(&'a')).method();
5361
| ^^^^^^
5462
|
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct A;
2+
3+
impl A {
4+
fn foo(self: Box<Self>) {}
5+
}
6+
7+
fn main() {
8+
A.foo(); //~ ERROR E0599
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0599]: no method named `foo` found for type `A` in the current scope
2+
--> $DIR/point-at-arbitrary-self-type-method.rs:8:7
3+
|
4+
LL | struct A;
5+
| --------- method `foo` not found for this
6+
...
7+
LL | fn foo(self: Box<Self>) {}
8+
| --- the method is available for `std::boxed::Box<A>` here
9+
...
10+
LL | A.foo();
11+
| ^^^
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0599`.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait B { fn foo(self: Box<Self>); }
2+
struct A;
3+
4+
impl B for A {
5+
fn foo(self: Box<Self>) {}
6+
}
7+
8+
fn main() {
9+
A.foo() //~ ERROR E0599
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0599]: no method named `foo` found for type `A` in the current scope
2+
--> $DIR/point-at-arbitrary-self-type-trait-method.rs:9:7
3+
|
4+
LL | trait B { fn foo(self: Box<Self>); }
5+
| --- the method is available for `std::boxed::Box<A>` here
6+
LL | struct A;
7+
| --------- method `foo` not found for this
8+
...
9+
LL | A.foo()
10+
| ^^^
11+
|
12+
= help: items from traits can only be used if the trait is implemented and in scope
13+
= note: the following trait defines an item `foo`, perhaps you need to implement it:
14+
candidate #1: `B`
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0599`.

src/test/ui/traits/trait-item-privacy.stderr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ error[E0599]: no method named `b` found for type `S` in the current scope
1717
LL | struct S;
1818
| --------- method `b` not found for this
1919
...
20+
LL | fn b(&self) { }
21+
| -
22+
| |
23+
| the method is available for `std::boxed::Box<S>` here
24+
| the method is available for `std::sync::Arc<S>` here
25+
| the method is available for `std::rc::Rc<S>` here
26+
...
2027
LL | S.b();
2128
| ^
2229
|

0 commit comments

Comments
 (0)