Skip to content

Commit a79e5e2

Browse files
committed
Rollup merge of rust-lang#48084 - cramertj:impl-trait-errors, r=nikomatsakis
Error on nested impl Trait and path projections from impl Trait cc rust-lang#34511 r? @nikomatsakis
2 parents 25ec810 + 9e9c55f commit a79e5e2

File tree

11 files changed

+295
-89
lines changed

11 files changed

+295
-89
lines changed

src/librustc_passes/ast_validation.rs

+135
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,141 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
419419
}
420420
}
421421

422+
// Bans nested `impl Trait`, e.g. `impl Into<impl Debug>`.
423+
// Nested `impl Trait` _is_ allowed in associated type position,
424+
// e.g `impl Iterator<Item=impl Debug>`
425+
struct NestedImplTraitVisitor<'a> {
426+
session: &'a Session,
427+
outer_impl_trait: Option<Span>,
428+
}
429+
430+
impl<'a> NestedImplTraitVisitor<'a> {
431+
fn with_impl_trait<F>(&mut self, outer_impl_trait: Option<Span>, f: F)
432+
where F: FnOnce(&mut NestedImplTraitVisitor<'a>)
433+
{
434+
let old_outer_impl_trait = self.outer_impl_trait;
435+
self.outer_impl_trait = outer_impl_trait;
436+
f(self);
437+
self.outer_impl_trait = old_outer_impl_trait;
438+
}
439+
}
440+
441+
442+
impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
443+
fn visit_ty(&mut self, t: &'a Ty) {
444+
if let TyKind::ImplTrait(_) = t.node {
445+
if let Some(outer_impl_trait) = self.outer_impl_trait {
446+
struct_span_err!(self.session, t.span, E0666,
447+
"nested `impl Trait` is not allowed")
448+
.span_label(outer_impl_trait, "outer `impl Trait`")
449+
.span_label(t.span, "nested `impl Trait` here")
450+
.emit();
451+
452+
}
453+
self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t));
454+
} else {
455+
visit::walk_ty(self, t);
456+
}
457+
}
458+
fn visit_path_parameters(&mut self, _: Span, path_parameters: &'a PathParameters) {
459+
match *path_parameters {
460+
PathParameters::AngleBracketed(ref params) => {
461+
for type_ in &params.types {
462+
self.visit_ty(type_);
463+
}
464+
for type_binding in &params.bindings {
465+
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
466+
// are allowed to contain nested `impl Trait`.
467+
self.with_impl_trait(None, |this| visit::walk_ty(this, &type_binding.ty));
468+
}
469+
}
470+
PathParameters::Parenthesized(ref params) => {
471+
for type_ in &params.inputs {
472+
self.visit_ty(type_);
473+
}
474+
if let Some(ref type_) = params.output {
475+
// `-> Foo` syntax is essentially an associated type binding,
476+
// so it is also allowed to contain nested `impl Trait`.
477+
self.with_impl_trait(None, |this| visit::walk_ty(this, type_));
478+
}
479+
}
480+
}
481+
}
482+
}
483+
484+
// Bans `impl Trait` in path projections like `<impl Iterator>::Item` or `Foo::Bar<impl Trait>`.
485+
struct ImplTraitProjectionVisitor<'a> {
486+
session: &'a Session,
487+
is_banned: bool,
488+
}
489+
490+
impl<'a> ImplTraitProjectionVisitor<'a> {
491+
fn with_ban<F>(&mut self, f: F)
492+
where F: FnOnce(&mut ImplTraitProjectionVisitor<'a>)
493+
{
494+
let old_is_banned = self.is_banned;
495+
self.is_banned = true;
496+
f(self);
497+
self.is_banned = old_is_banned;
498+
}
499+
}
500+
501+
impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
502+
fn visit_ty(&mut self, t: &'a Ty) {
503+
match t.node {
504+
TyKind::ImplTrait(_) => {
505+
if self.is_banned {
506+
struct_span_err!(self.session, t.span, E0667,
507+
"`impl Trait` is not allowed in path parameters")
508+
.emit();
509+
}
510+
}
511+
TyKind::Path(ref qself, ref path) => {
512+
// We allow these:
513+
// - `Option<impl Trait>`
514+
// - `option::Option<impl Trait>`
515+
// - `option::Option<T>::Foo<impl Trait>
516+
//
517+
// But not these:
518+
// - `<impl Trait>::Foo`
519+
// - `option::Option<impl Trait>::Foo`.
520+
//
521+
// To implement this, we disallow `impl Trait` from `qself`
522+
// (for cases like `<impl Trait>::Foo>`)
523+
// but we allow `impl Trait` in `PathParameters`
524+
// iff there are no more PathSegments.
525+
if let Some(ref qself) = *qself {
526+
// `impl Trait` in `qself` is always illegal
527+
self.with_ban(|this| this.visit_ty(&qself.ty));
528+
}
529+
530+
for (i, segment) in path.segments.iter().enumerate() {
531+
// Allow `impl Trait` iff we're on the final path segment
532+
if i == (path.segments.len() - 1) {
533+
visit::walk_path_segment(self, path.span, segment);
534+
} else {
535+
self.with_ban(|this|
536+
visit::walk_path_segment(this, path.span, segment));
537+
}
538+
}
539+
}
540+
_ => visit::walk_ty(self, t),
541+
}
542+
}
543+
}
544+
422545
pub fn check_crate(session: &Session, krate: &Crate) {
546+
visit::walk_crate(
547+
&mut NestedImplTraitVisitor {
548+
session,
549+
outer_impl_trait: None,
550+
}, krate);
551+
552+
visit::walk_crate(
553+
&mut ImplTraitProjectionVisitor {
554+
session,
555+
is_banned: false,
556+
}, krate);
557+
423558
visit::walk_crate(&mut AstValidator { session: session }, krate)
424559
}

src/librustc_passes/diagnostics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,6 @@ register_diagnostics! {
304304
E0567, // auto traits can not have generic parameters
305305
E0568, // auto traits can not have super traits
306306
E0642, // patterns aren't allowed in methods without bodies
307+
E0666, // nested `impl Trait` is illegal
308+
E0667, // `impl Trait` in projections
307309
}

src/libsyntax/feature_gate.rs

+1-69
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,6 @@ declare_features! (
432432
// `foo.rs` as an alternative to `foo/mod.rs`
433433
(active, non_modrs_mods, "1.24.0", Some(44660)),
434434

435-
// Nested `impl Trait`
436-
(active, nested_impl_trait, "1.24.0", Some(34511)),
437-
438435
// Termination trait in main (RFC 1937)
439436
(active, termination_trait, "1.24.0", Some(43301)),
440437

@@ -1352,73 +1349,8 @@ fn contains_novel_literal(item: &ast::MetaItem) -> bool {
13521349
}
13531350
}
13541351

1355-
// Bans nested `impl Trait`, e.g. `impl Into<impl Debug>`.
1356-
// Nested `impl Trait` _is_ allowed in associated type position,
1357-
// e.g `impl Iterator<Item=impl Debug>`
1358-
struct NestedImplTraitVisitor<'a> {
1359-
context: &'a Context<'a>,
1360-
is_in_impl_trait: bool,
1361-
}
1362-
1363-
impl<'a> NestedImplTraitVisitor<'a> {
1364-
fn with_impl_trait<F>(&mut self, is_in_impl_trait: bool, f: F)
1365-
where F: FnOnce(&mut NestedImplTraitVisitor<'a>)
1366-
{
1367-
let old_is_in_impl_trait = self.is_in_impl_trait;
1368-
self.is_in_impl_trait = is_in_impl_trait;
1369-
f(self);
1370-
self.is_in_impl_trait = old_is_in_impl_trait;
1371-
}
1372-
}
1373-
1374-
1375-
impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
1376-
fn visit_ty(&mut self, t: &'a ast::Ty) {
1377-
if let ast::TyKind::ImplTrait(_) = t.node {
1378-
if self.is_in_impl_trait {
1379-
gate_feature_post!(&self, nested_impl_trait, t.span,
1380-
"nested `impl Trait` is experimental"
1381-
);
1382-
}
1383-
self.with_impl_trait(true, |this| visit::walk_ty(this, t));
1384-
} else {
1385-
visit::walk_ty(self, t);
1386-
}
1387-
}
1388-
fn visit_path_parameters(&mut self, _: Span, path_parameters: &'a ast::PathParameters) {
1389-
match *path_parameters {
1390-
ast::PathParameters::AngleBracketed(ref params) => {
1391-
for type_ in &params.types {
1392-
self.visit_ty(type_);
1393-
}
1394-
for type_binding in &params.bindings {
1395-
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
1396-
// are allowed to contain nested `impl Trait`.
1397-
self.with_impl_trait(false, |this| visit::walk_ty(this, &type_binding.ty));
1398-
}
1399-
}
1400-
ast::PathParameters::Parenthesized(ref params) => {
1401-
for type_ in &params.inputs {
1402-
self.visit_ty(type_);
1403-
}
1404-
if let Some(ref type_) = params.output {
1405-
// `-> Foo` syntax is essentially an associated type binding,
1406-
// so it is also allowed to contain nested `impl Trait`.
1407-
self.with_impl_trait(false, |this| visit::walk_ty(this, type_));
1408-
}
1409-
}
1410-
}
1411-
}
1412-
}
1413-
14141352
impl<'a> PostExpansionVisitor<'a> {
1415-
fn whole_crate_feature_gates(&mut self, krate: &ast::Crate) {
1416-
visit::walk_crate(
1417-
&mut NestedImplTraitVisitor {
1418-
context: self.context,
1419-
is_in_impl_trait: false,
1420-
}, krate);
1421-
1353+
fn whole_crate_feature_gates(&mut self, _krate: &ast::Crate) {
14221354
for &(ident, span) in &*self.context.parse_sess.non_modrs_mods.borrow() {
14231355
if !span.allows_unstable() {
14241356
let cx = &self.context;

src/test/compile-fail/impl-trait/where-allowed.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! A simple test for testing many permutations of allowedness of
1212
//! impl Trait
13-
#![feature(conservative_impl_trait, nested_impl_trait, universal_impl_trait, dyn_trait)]
13+
#![feature(conservative_impl_trait, universal_impl_trait, dyn_trait)]
1414
use std::fmt::Debug;
1515

1616
// Allowed
@@ -60,6 +60,7 @@ fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() }
6060
// Disallowed
6161
fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() }
6262
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
63+
//~^^ ERROR nested `impl Trait` is not allowed
6364

6465
// Disallowed
6566
fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
@@ -68,6 +69,7 @@ fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
6869
// Disallowed
6970
fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() }
7071
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
72+
//~^^ ERROR nested `impl Trait` is not allowed
7173

7274
// Disallowed
7375
fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() }

src/test/run-pass/impl-trait/lifetimes.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(conservative_impl_trait, underscore_lifetimes, universal_impl_trait, nested_impl_trait)]
11+
#![feature(conservative_impl_trait, underscore_lifetimes, universal_impl_trait)]
1212
#![allow(warnings)]
1313

1414
use std::fmt::Debug;
@@ -63,12 +63,11 @@ fn pass_through_elision_with_fn_ptr(x: &fn(&u32) -> &u32) -> impl Into<&fn(&u32)
6363

6464
fn pass_through_elision_with_fn_path<T: Fn(&u32) -> &u32>(
6565
x: &T
66-
) -> impl Into<&impl Fn(&u32) -> &u32> { x }
66+
) -> &impl Fn(&u32) -> &u32 { x }
6767

68-
fn foo(x: &impl Debug) -> impl Into<&impl Debug> { x }
69-
fn foo_explicit_lifetime<'a>(x: &'a impl Debug) -> impl Into<&'a impl Debug> { x }
70-
fn foo_no_outer_impl(x: &impl Debug) -> &impl Debug { x }
71-
fn foo_explicit_arg<T: Debug>(x: &T) -> impl Into<&impl Debug> { x }
68+
fn foo(x: &impl Debug) -> &impl Debug { x }
69+
fn foo_explicit_lifetime<'a>(x: &'a impl Debug) -> &'a impl Debug { x }
70+
fn foo_explicit_arg<T: Debug>(x: &T) -> &impl Debug { x }
7271

7372
fn mixed_lifetimes<'a>() -> impl for<'b: 'a> Fn(&'b u32) { |_| () }
7473
fn mixed_as_static() -> impl Fn(&'static u32) { mixed_lifetimes() }

src/test/ui/error-codes/E0657.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010
#![allow(warnings)]
11-
#![feature(conservative_impl_trait, nested_impl_trait)]
11+
#![feature(conservative_impl_trait)]
1212

1313
trait Id<T> {}
1414
trait Lt<'a> {}
@@ -17,7 +17,7 @@ impl<'a> Lt<'a> for () {}
1717
impl<T> Id<T> for T {}
1818

1919
fn free_fn_capture_hrtb_in_impl_trait()
20-
-> impl for<'a> Id<impl Lt<'a>>
20+
-> Box<for<'a> Id<impl Lt<'a>>>
2121
//~^ ERROR `impl Trait` can only capture lifetimes bound at the fn or impl level [E0657]
2222
{
2323
()
@@ -26,7 +26,7 @@ fn free_fn_capture_hrtb_in_impl_trait()
2626
struct Foo;
2727
impl Foo {
2828
fn impl_fn_capture_hrtb_in_impl_trait()
29-
-> impl for<'a> Id<impl Lt<'a>>
29+
-> Box<for<'a> Id<impl Lt<'a>>>
3030
//~^ ERROR `impl Trait` can only capture lifetimes bound at the fn or impl level
3131
{
3232
()

src/test/ui/error-codes/E0657.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error[E0657]: `impl Trait` can only capture lifetimes bound at the fn or impl level
2-
--> $DIR/E0657.rs:20:32
2+
--> $DIR/E0657.rs:20:31
33
|
4-
20 | -> impl for<'a> Id<impl Lt<'a>>
5-
| ^^
4+
20 | -> Box<for<'a> Id<impl Lt<'a>>>
5+
| ^^
66

77
error[E0657]: `impl Trait` can only capture lifetimes bound at the fn or impl level
8-
--> $DIR/E0657.rs:29:36
8+
--> $DIR/E0657.rs:29:35
99
|
10-
29 | -> impl for<'a> Id<impl Lt<'a>>
11-
| ^^
10+
29 | -> Box<for<'a> Id<impl Lt<'a>>>
11+
| ^^
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/impl_trait_projections.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2018 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+
#![feature(dyn_trait, conservative_impl_trait, universal_impl_trait)]
11+
12+
use std::fmt::Debug;
13+
use std::option;
14+
15+
fn parametrized_type_is_allowed() -> Option<impl Debug> {
16+
Some(5i32)
17+
}
18+
19+
fn path_parametrized_type_is_allowed() -> option::Option<impl Debug> {
20+
Some(5i32)
21+
}
22+
23+
fn projection_is_disallowed(x: impl Iterator) -> <impl Iterator>::Item {
24+
//~^ ERROR `impl Trait` is not allowed in path parameters
25+
//~^^ ERROR ambiguous associated type
26+
x.next().unwrap()
27+
}
28+
29+
fn projection_with_named_trait_is_disallowed(x: impl Iterator)
30+
-> <impl Iterator as Iterator>::Item
31+
//~^ ERROR `impl Trait` is not allowed in path parameters
32+
{
33+
x.next().unwrap()
34+
}
35+
36+
fn projection_with_named_trait_inside_path_is_disallowed()
37+
-> <::std::ops::Range<impl Debug> as Iterator>::Item
38+
//~^ ERROR `impl Trait` is not allowed in path parameters
39+
{
40+
(1i32..100).next().unwrap()
41+
}
42+
43+
fn projection_from_impl_trait_inside_dyn_trait_is_disallowed()
44+
-> <dyn Iterator<Item = impl Debug> as Iterator>::Item
45+
//~^ ERROR `impl Trait` is not allowed in path parameters
46+
{
47+
panic!()
48+
}
49+
50+
fn main() {}

0 commit comments

Comments
 (0)