Skip to content

Commit 558cbfb

Browse files
committed
prohibit turbofish in impl Trait methods
1 parent a76bff8 commit 558cbfb

File tree

5 files changed

+75
-31
lines changed

5 files changed

+75
-31
lines changed

src/librustc_typeck/check/method/confirm.rs

+29-23
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ pub struct ConfirmResult<'tcx> {
4646
}
4747

4848
impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
49-
pub fn confirm_method(&self,
50-
span: Span,
51-
self_expr: &'gcx hir::Expr,
52-
call_expr: &'gcx hir::Expr,
53-
unadjusted_self_ty: Ty<'tcx>,
54-
pick: probe::Pick<'tcx>,
55-
segment: &hir::PathSegment)
56-
-> ConfirmResult<'tcx> {
57-
debug!("confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
58-
unadjusted_self_ty,
59-
pick,
60-
segment.parameters);
49+
pub fn confirm_method(
50+
&self,
51+
span: Span,
52+
self_expr: &'gcx hir::Expr,
53+
call_expr: &'gcx hir::Expr,
54+
unadjusted_self_ty: Ty<'tcx>,
55+
pick: probe::Pick<'tcx>,
56+
segment: &hir::PathSegment,
57+
) -> ConfirmResult<'tcx> {
58+
debug!(
59+
"confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
60+
unadjusted_self_ty,
61+
pick,
62+
segment.parameters,
63+
);
6164

6265
let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
6366
confirm_cx.confirm(unadjusted_self_ty, pick, segment)
@@ -78,11 +81,12 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
7881
}
7982
}
8083

81-
fn confirm(&mut self,
82-
unadjusted_self_ty: Ty<'tcx>,
83-
pick: probe::Pick<'tcx>,
84-
segment: &hir::PathSegment)
85-
-> ConfirmResult<'tcx> {
84+
fn confirm(
85+
&mut self,
86+
unadjusted_self_ty: Ty<'tcx>,
87+
pick: probe::Pick<'tcx>,
88+
segment: &hir::PathSegment,
89+
) -> ConfirmResult<'tcx> {
8690
// Adjust the self expression the user provided and obtain the adjusted type.
8791
let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
8892

@@ -300,17 +304,19 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
300304
})
301305
}
302306

303-
fn instantiate_method_substs(&mut self,
304-
pick: &probe::Pick<'tcx>,
305-
segment: &hir::PathSegment,
306-
parent_substs: &Substs<'tcx>)
307-
-> &'tcx Substs<'tcx> {
307+
fn instantiate_method_substs(
308+
&mut self,
309+
pick: &probe::Pick<'tcx>,
310+
segment: &hir::PathSegment,
311+
parent_substs: &Substs<'tcx>,
312+
) -> &'tcx Substs<'tcx> {
308313
// Determine the values for the generic parameters of the method.
309314
// If they were not explicitly supplied, just construct fresh
310315
// variables.
311316
let method_generics = self.tcx.generics_of(pick.item.def_id);
312317
let mut fn_segment = Some((segment, method_generics));
313-
self.fcx.check_path_parameter_count(self.span, &mut fn_segment, true, false);
318+
let supress_mismatch = self.fcx.check_impl_trait(self.span, fn_segment);
319+
self.fcx.check_path_parameter_count(self.span, &mut fn_segment, true, supress_mismatch);
314320

315321
// Create subst for early-bound lifetime parameters, combining
316322
// parameters from the type and those from the method.

src/librustc_typeck/check/method/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
179179

180180
self.tcx.check_stability(pick.item.def_id, Some(call_expr.id), span);
181181

182-
let result = self.confirm_method(span,
183-
self_expr,
184-
call_expr,
185-
self_ty,
186-
pick.clone(),
187-
segment);
182+
let result = self.confirm_method(
183+
span,
184+
self_expr,
185+
call_expr,
186+
self_ty,
187+
pick.clone(),
188+
segment,
189+
);
188190

189191
if result.illegal_sized_bound {
190192
// We probe again, taking all traits into account (not only those in scope).

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4734,7 +4734,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
47344734
// variables. If the user provided some types, we may still need
47354735
// to add defaults. If the user provided *too many* types, that's
47364736
// a problem.
4737-
let supress_mismatch = self.check_impl_trait(span, &mut fn_segment);
4737+
let supress_mismatch = self.check_impl_trait(span, fn_segment);
47384738
self.check_path_parameter_count(span, &mut type_segment, false, supress_mismatch);
47394739
self.check_path_parameter_count(span, &mut fn_segment, false, supress_mismatch);
47404740

@@ -5019,7 +5019,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
50195019
/// Report error if there is an explicit type parameter when using `impl Trait`.
50205020
fn check_impl_trait(&self,
50215021
span: Span,
5022-
segment: &mut Option<(&hir::PathSegment, &ty::Generics)>)
5022+
segment: Option<(&hir::PathSegment, &ty::Generics)>)
50235023
-> bool {
50245024
let segment = segment.map(|(path_segment, generics)| {
50255025
let explicit = !path_segment.infer_types;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::any::Any;
12+
pub struct EventHandler {
13+
}
14+
15+
impl EventHandler
16+
{
17+
pub fn handle_event<T: Any>(&mut self, _efunc: impl FnMut(T)) {}
18+
}
19+
20+
struct TestEvent(i32);
21+
22+
fn main() {
23+
let mut evt = EventHandler {};
24+
evt.handle_event::<TestEvent, fn(TestEvent)>(|_evt| {
25+
//~^ ERROR cannot provide explicit type parameters
26+
});
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0632]: cannot provide explicit type parameters when `impl Trait` is used in argument position.
2+
--> $DIR/universal-turbofish-in-method-issue-50950.rs:24:9
3+
|
4+
LL | evt.handle_event::<TestEvent, fn(TestEvent)>(|_evt| {
5+
| ^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0632`.

0 commit comments

Comments
 (0)