Skip to content

Commit 0915d89

Browse files
committed
Auto merge of #41856 - qnighy:prohibit-parenthesized-params-in-more-types, r=arielb1
Prohibit parenthesized params in more types. Prohibit parenthesized parameters in primitive types, type parameters, `Self`, etc. Fixes #32995.
2 parents 4d09a0e + e8137d7 commit 0915d89

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

src/librustc_typeck/astconv.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
156156
match item_segment.parameters {
157157
hir::AngleBracketedParameters(_) => {}
158158
hir::ParenthesizedParameters(..) => {
159-
struct_span_err!(tcx.sess, span, E0214,
160-
"parenthesized parameters may only be used with a trait")
161-
.span_label(span, "only traits may use parentheses")
162-
.emit();
159+
self.prohibit_parenthesized_params(item_segment);
163160

164161
return Substs::for_item(tcx, def_id, |_, _| {
165162
tcx.types.re_static
@@ -370,6 +367,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
370367
self_ty: Ty<'tcx>)
371368
-> ty::TraitRef<'tcx>
372369
{
370+
self.prohibit_type_params(trait_ref.path.segments.split_last().unwrap().1);
371+
373372
let trait_def_id = self.trait_def_id(trait_ref);
374373
self.ast_path_to_mono_trait_ref(trait_ref.path.span,
375374
trait_def_id,
@@ -402,6 +401,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
402401

403402
debug!("ast_path_to_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id);
404403

404+
self.prohibit_type_params(trait_ref.path.segments.split_last().unwrap().1);
405+
405406
let (substs, assoc_bindings) =
406407
self.create_substs_for_ast_trait_ref(trait_ref.path.span,
407408
trait_def_id,
@@ -623,6 +624,13 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
623624
dummy_self,
624625
&mut projection_bounds);
625626

627+
for trait_bound in trait_bounds[1..].iter() {
628+
// Sanity check for non-principal trait bounds
629+
self.instantiate_poly_trait_ref(trait_bound,
630+
dummy_self,
631+
&mut vec![]);
632+
}
633+
626634
let (auto_traits, trait_bounds) = split_auto_traits(tcx, &trait_bounds[1..]);
627635

628636
if !trait_bounds.is_empty() {
@@ -937,6 +945,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
937945

938946
pub fn prohibit_type_params(&self, segments: &[hir::PathSegment]) {
939947
for segment in segments {
948+
if let hir::ParenthesizedParameters(_) = segment.parameters {
949+
self.prohibit_parenthesized_params(segment);
950+
break;
951+
}
940952
for typ in segment.parameters.types() {
941953
struct_span_err!(self.tcx().sess, typ.span, E0109,
942954
"type parameters are not allowed on this type")
@@ -959,6 +971,15 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
959971
}
960972
}
961973

974+
pub fn prohibit_parenthesized_params(&self, segment: &hir::PathSegment) {
975+
if let hir::ParenthesizedParameters(ref data) = segment.parameters {
976+
struct_span_err!(self.tcx().sess, data.span, E0214,
977+
"parenthesized parameters may only be used with a trait")
978+
.span_label(data.span, "only traits may use parentheses")
979+
.emit();
980+
}
981+
}
982+
962983
pub fn prohibit_projection(&self, span: Span) {
963984
let mut err = struct_span_err!(self.tcx().sess, span, E0229,
964985
"associated type bindings are not allowed here");

src/librustc_typeck/check/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4523,7 +4523,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
45234523
(&data.lifetimes[..], &data.types[..], data.infer_types, &data.bindings[..])
45244524
}
45254525
Some(&hir::ParenthesizedParameters(_)) => {
4526-
span_bug!(span, "parenthesized parameters cannot appear in ExprPath");
4526+
AstConv::prohibit_parenthesized_params(self, &segment.as_ref().unwrap().0);
4527+
(&[][..], &[][..], true, &[][..])
45274528
}
45284529
None => (&[][..], &[][..], true, &[][..])
45294530
}

src/test/compile-fail/issue-22560.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ type Test = Add +
1919
//~| ERROR E0191
2020
//~| NOTE missing associated type `Output` value
2121
Sub;
22-
//~^ ERROR E0225
22+
//~^ ERROR E0393
23+
//~| NOTE missing reference to `RHS`
24+
//~| NOTE because of the default `Self` reference, type parameters must be specified on object types
25+
//~| ERROR E0225
2326
//~| NOTE non-Send/Sync additional trait
2427

2528
fn main() { }
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 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+
#![feature(conservative_impl_trait)]
12+
13+
fn main() {
14+
{ fn f<X: ::std::marker()::Send>() {} }
15+
//~^ ERROR parenthesized parameters may only be used with a trait
16+
17+
{ fn f() -> impl ::std::marker()::Send { } }
18+
//~^ ERROR parenthesized parameters may only be used with a trait
19+
}
20+
21+
#[derive(Clone)]
22+
struct X;
23+
24+
impl ::std::marker()::Copy for X {}
25+
//~^ ERROR parenthesized parameters may only be used with a trait

src/test/compile-fail/issue-32995.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2017 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+
fn main() {
12+
let s: String() = String::from("foo");
13+
//~^ ERROR parenthesized parameters may only be used with a trait
14+
15+
let x: usize() = 1;
16+
//~^ ERROR parenthesized parameters may only be used with a trait
17+
18+
let b: ::std::boxed()::Box<_> = Box::new(1);
19+
//~^ ERROR parenthesized parameters may only be used with a trait
20+
21+
macro_rules! pathexpr {
22+
($p:path) => { $p }
23+
}
24+
25+
let p = pathexpr!(::std::str()::from_utf8)(b"foo").unwrap();
26+
//~^ ERROR parenthesized parameters may only be used with a trait
27+
28+
let p = pathexpr!(::std::str::from_utf8())(b"foo").unwrap();
29+
//~^ ERROR parenthesized parameters may only be used with a trait
30+
31+
let o : Box<::std::marker()::Send> = Box::new(1);
32+
//~^ ERROR parenthesized parameters may only be used with a trait
33+
34+
let o : Box<Send + ::std::marker()::Sync> = Box::new(1);
35+
//~^ ERROR parenthesized parameters may only be used with a trait
36+
}
37+
38+
fn foo<X:Default>() {
39+
let d : X() = Default::default();
40+
//~^ ERROR parenthesized parameters may only be used with a trait
41+
}

0 commit comments

Comments
 (0)