Skip to content

Commit 03bed65

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 be5f4fe + 9999378 commit 03bed65

File tree

7 files changed

+148
-6
lines changed

7 files changed

+148
-6
lines changed

src/librustc/lint/builtin.rs

+7
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ declare_lint! {
242242
"detects missing fragment specifiers in unused `macro_rules!` patterns"
243243
}
244244

245+
declare_lint! {
246+
pub PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
247+
Warn,
248+
"detects parenthesized generic parameters in type and module names"
249+
}
250+
245251
declare_lint! {
246252
pub DEPRECATED,
247253
Warn,
@@ -293,6 +299,7 @@ impl LintPass for HardwiredLints {
293299
LEGACY_IMPORTS,
294300
LEGACY_CONSTRUCTOR_VISIBILITY,
295301
MISSING_FRAGMENT_SPECIFIER,
302+
PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
296303
DEPRECATED
297304
)
298305
}

src/librustc_lint/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
252252
id: LintId::of(MISSING_FRAGMENT_SPECIFIER),
253253
reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
254254
},
255+
FutureIncompatibleInfo {
256+
id: LintId::of(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES),
257+
reference: "issue #42238 <https://github.com/rust-lang/rust/issues/42238>",
258+
},
255259
FutureIncompatibleInfo {
256260
id: LintId::of(ANONYMOUS_PARAMETERS),
257261
reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",

src/librustc_typeck/astconv.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc::ty::subst::{Kind, Subst, Substs};
2222
use rustc::traits;
2323
use rustc::ty::{self, Ty, TyCtxt, ToPredicate, TypeFoldable};
2424
use rustc::ty::wf::object_region_bounds;
25+
use rustc::lint::builtin::PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES;
2526
use rustc_back::slice;
2627
use require_c_abi_if_variadic;
2728
use util::common::{ErrorReported, FN_OUTPUT_NAME};
@@ -156,10 +157,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
156157
match item_segment.parameters {
157158
hir::AngleBracketedParameters(_) => {}
158159
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();
160+
self.prohibit_parenthesized_params(item_segment, true);
163161

164162
return Substs::for_item(tcx, def_id, |_, _| {
165163
tcx.types.re_static
@@ -370,6 +368,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
370368
self_ty: Ty<'tcx>)
371369
-> ty::TraitRef<'tcx>
372370
{
371+
self.prohibit_type_params(trait_ref.path.segments.split_last().unwrap().1);
372+
373373
let trait_def_id = self.trait_def_id(trait_ref);
374374
self.ast_path_to_mono_trait_ref(trait_ref.path.span,
375375
trait_def_id,
@@ -402,6 +402,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
402402

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

405+
self.prohibit_type_params(trait_ref.path.segments.split_last().unwrap().1);
406+
405407
let (substs, assoc_bindings) =
406408
self.create_substs_for_ast_trait_ref(trait_ref.path.span,
407409
trait_def_id,
@@ -623,6 +625,13 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
623625
dummy_self,
624626
&mut projection_bounds);
625627

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

628637
if !trait_bounds.is_empty() {
@@ -938,6 +947,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
938947

939948
pub fn prohibit_type_params(&self, segments: &[hir::PathSegment]) {
940949
for segment in segments {
950+
if let hir::ParenthesizedParameters(_) = segment.parameters {
951+
self.prohibit_parenthesized_params(segment, false);
952+
break;
953+
}
941954
for typ in segment.parameters.types() {
942955
struct_span_err!(self.tcx().sess, typ.span, E0109,
943956
"type parameters are not allowed on this type")
@@ -960,6 +973,21 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
960973
}
961974
}
962975

976+
pub fn prohibit_parenthesized_params(&self, segment: &hir::PathSegment, emit_error: bool) {
977+
if let hir::ParenthesizedParameters(ref data) = segment.parameters {
978+
if emit_error {
979+
struct_span_err!(self.tcx().sess, data.span, E0214,
980+
"parenthesized parameters may only be used with a trait")
981+
.span_label(data.span, "only traits may use parentheses")
982+
.emit();
983+
} else {
984+
let msg = "parenthesized parameters may only be used with a trait".to_string();
985+
self.tcx().sess.add_lint(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
986+
ast::CRATE_NODE_ID, data.span, msg);
987+
}
988+
}
989+
}
990+
963991
pub fn prohibit_projection(&self, span: Span) {
964992
let mut err = struct_span_err!(self.tcx().sess, span, E0229,
965993
"associated type bindings are not allowed here");

src/librustc_typeck/check/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4536,7 +4536,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
45364536
(&data.lifetimes[..], &data.types[..], data.infer_types, &data.bindings[..])
45374537
}
45384538
Some(&hir::ParenthesizedParameters(_)) => {
4539-
span_bug!(span, "parenthesized parameters cannot appear in ExprPath");
4539+
AstConv::prohibit_parenthesized_params(self, &segment.as_ref().unwrap().0,
4540+
false);
4541+
(&[][..], &[][..], true, &[][..])
45404542
}
45414543
None => (&[][..], &[][..], true, &[][..])
45424544
}

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() { }
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
#![deny(parenthesized_params_in_types_and_modules)]
12+
//~^ NOTE lint level defined here
13+
//~| NOTE lint level defined here
14+
//~| NOTE lint level defined here
15+
#![allow(dead_code, unused_variables)]
16+
#![feature(conservative_impl_trait)]
17+
18+
fn main() {
19+
{ fn f<X: ::std::marker()::Send>() {} }
20+
//~^ ERROR parenthesized parameters may only be used with a trait
21+
//~| WARN previously accepted
22+
//~| NOTE issue #42238
23+
24+
{ fn f() -> impl ::std::marker()::Send { } }
25+
//~^ ERROR parenthesized parameters may only be used with a trait
26+
//~| WARN previously accepted
27+
//~| NOTE issue #42238
28+
}
29+
30+
#[derive(Clone)]
31+
struct X;
32+
33+
impl ::std::marker()::Copy for X {}
34+
//~^ ERROR parenthesized parameters may only be used with a trait
35+
//~| WARN previously accepted
36+
//~| NOTE issue #42238

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

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
#![deny(parenthesized_params_in_types_and_modules)]
12+
//~^ NOTE lint level defined here
13+
//~| NOTE lint level defined here
14+
//~| NOTE lint level defined here
15+
//~| NOTE lint level defined here
16+
//~| NOTE lint level defined here
17+
//~| NOTE lint level defined here
18+
//~| NOTE lint level defined here
19+
#![allow(dead_code, unused_variables)]
20+
21+
fn main() {
22+
let x: usize() = 1;
23+
//~^ ERROR parenthesized parameters may only be used with a trait
24+
//~| WARN previously accepted
25+
//~| NOTE issue #42238
26+
27+
let b: ::std::boxed()::Box<_> = Box::new(1);
28+
//~^ ERROR parenthesized parameters may only be used with a trait
29+
//~| WARN previously accepted
30+
//~| NOTE issue #42238
31+
32+
macro_rules! pathexpr {
33+
($p:path) => { $p }
34+
}
35+
36+
let p = pathexpr!(::std::str()::from_utf8)(b"foo").unwrap();
37+
//~^ ERROR parenthesized parameters may only be used with a trait
38+
//~| WARN previously accepted
39+
//~| NOTE issue #42238
40+
41+
let p = pathexpr!(::std::str::from_utf8())(b"foo").unwrap();
42+
//~^ ERROR parenthesized parameters may only be used with a trait
43+
//~| WARN previously accepted
44+
//~| NOTE issue #42238
45+
46+
let o : Box<::std::marker()::Send> = Box::new(1);
47+
//~^ ERROR parenthesized parameters may only be used with a trait
48+
//~| WARN previously accepted
49+
//~| NOTE issue #42238
50+
51+
let o : Box<Send + ::std::marker()::Sync> = Box::new(1);
52+
//~^ ERROR parenthesized parameters may only be used with a trait
53+
//~| WARN previously accepted
54+
//~| NOTE issue #42238
55+
}
56+
57+
fn foo<X:Default>() {
58+
let d : X() = Default::default();
59+
//~^ ERROR parenthesized parameters may only be used with a trait
60+
//~| WARN previously accepted
61+
//~| NOTE issue #42238
62+
}

0 commit comments

Comments
 (0)