Skip to content

Commit 45a9d41

Browse files
committed
Auto merge of #53016 - scottmcm:impl-header-lifetime-elision, r=nikomatsakis
Extract impl_header_lifetime_elision out of in_band_lifetimes This way we can experiment with `impl Debug for &MyType` separately from `impl Debug for &'a MyType`. I can't say I know what the code in here is doing, so please let me know if there's a better way 🙂 I marked this as enabled in 2018 so that edition code continues to work without another flag. Actual feature PR #49251; Tracking Issue #15872; In-band lifetimes tracking issue #44524. cc @aturon, per discussion on discord earlier cc @cramertj & @nikomatsakis, who actually wrote these features
2 parents cf84056 + 1c7af27 commit 45a9d41

21 files changed

+88
-32
lines changed

src/librustc/hir/lowering.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ pub struct LoweringContext<'a> {
123123
// Whether or not in-band lifetimes are being collected. This is used to
124124
// indicate whether or not we're in a place where new lifetimes will result
125125
// in in-band lifetime definitions, such a function or an impl header.
126-
// This will always be false unless the `in_band_lifetimes` feature is
127-
// enabled.
126+
// This will always be false unless the `in_band_lifetimes` or
127+
// `impl_header_lifetime_elision` feature is enabled.
128128
is_collecting_in_band_lifetimes: bool,
129129

130130
// Currently in-scope lifetimes defined in impl headers, fn headers, or HRTB.
@@ -658,9 +658,11 @@ impl<'a> LoweringContext<'a> {
658658
assert!(self.lifetimes_to_define.is_empty());
659659
let old_anonymous_lifetime_mode = self.anonymous_lifetime_mode;
660660

661-
self.is_collecting_in_band_lifetimes = self.sess.features_untracked().in_band_lifetimes;
662-
if self.is_collecting_in_band_lifetimes {
661+
if self.sess.features_untracked().impl_header_lifetime_elision {
663662
self.anonymous_lifetime_mode = anonymous_lifetime_mode;
663+
self.is_collecting_in_band_lifetimes = true;
664+
} else if self.sess.features_untracked().in_band_lifetimes {
665+
self.is_collecting_in_band_lifetimes = true;
664666
}
665667

666668
let (in_band_ty_params, res) = f(self);
@@ -718,6 +720,10 @@ impl<'a> LoweringContext<'a> {
718720
return;
719721
}
720722

723+
if !self.sess.features_untracked().in_band_lifetimes {
724+
return;
725+
}
726+
721727
if self.in_scope_lifetimes.contains(&ident.modern()) {
722728
return;
723729
}

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#![feature(step_trait)]
6969
#![feature(integer_atomics)]
7070
#![feature(test)]
71+
#![cfg_attr(not(stage0), feature(impl_header_lifetime_elision))]
7172
#![feature(in_band_lifetimes)]
7273
#![feature(macro_at_most_once_rep)]
7374
#![feature(crate_in_paths)]

src/libsyntax/feature_gate.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ declare_features! (
380380
(active, crate_in_paths, "1.23.0", Some(45477), Some(Edition::Edition2018)),
381381

382382
// In-band lifetime bindings (e.g. `fn foo(x: &'a u8) -> &'a u8`)
383-
(active, in_band_lifetimes, "1.23.0", Some(44524), Some(Edition::Edition2018)),
383+
(active, in_band_lifetimes, "1.23.0", Some(44524), None),
384384

385385
// generic associated types (RFC 1598)
386386
(active, generic_associated_types, "1.23.0", Some(44265), None),
@@ -481,6 +481,10 @@ declare_features! (
481481
(active, alloc_error_handler, "1.29.0", Some(51540), None),
482482

483483
(active, abi_amdgpu_kernel, "1.29.0", Some(51575), None),
484+
485+
// impl<I:Iterator> Iterator for &mut Iterator
486+
// impl Debug for Foo<'_>
487+
(active, impl_header_lifetime_elision, "1.30.0", Some(15872), Some(Edition::Edition2018)),
484488
);
485489

486490
declare_features! (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#![allow(warnings)]
12+
13+
// Make sure this related feature didn't accidentally enable this
14+
#![feature(in_band_lifetimes)]
15+
16+
trait MyTrait<'a> { }
17+
18+
impl MyTrait<'a> for &u32 { }
19+
//~^ ERROR missing lifetime specifier
20+
21+
struct MyStruct;
22+
trait MarkerTrait {}
23+
24+
impl MarkerTrait for &'_ MyStruct { }
25+
//~^ ERROR missing lifetime specifier
26+
27+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0106]: missing lifetime specifier
2+
--> $DIR/feature-gate-impl_header_lifetime_elision-with-in_band.rs:18:22
3+
|
4+
LL | impl MyTrait<'a> for &u32 { }
5+
| ^ expected lifetime parameter
6+
7+
error[E0106]: missing lifetime specifier
8+
--> $DIR/feature-gate-impl_header_lifetime_elision-with-in_band.rs:24:23
9+
|
10+
LL | impl MarkerTrait for &'_ MyStruct { }
11+
| ^^ expected lifetime parameter
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0106`.

src/test/ui/feature-gate-in_band_lifetimes-impl.stderr renamed to src/test/ui/feature-gate-impl_header_lifetime_elision.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0106]: missing lifetime specifier
2-
--> $DIR/feature-gate-in_band_lifetimes-impl.rs:15:26
2+
--> $DIR/feature-gate-impl_header_lifetime_elision.rs:15:26
33
|
44
LL | impl<'a> MyTrait<'a> for &u32 { }
55
| ^ expected lifetime parameter
66

77
error[E0106]: missing lifetime specifier
8-
--> $DIR/feature-gate-in_band_lifetimes-impl.rs:18:18
8+
--> $DIR/feature-gate-impl_header_lifetime_elision.rs:18:18
99
|
1010
LL | impl<'a> MyTrait<'_> for &'a f32 { }
1111
| ^^ expected lifetime parameter

src/test/ui/feature-gate-in_band_lifetimes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#![allow(warnings)]
1212

13+
// Make sure this related feature didn't accidentally enable this
14+
#![feature(impl_header_lifetime_elision)]
15+
1316
fn foo(x: &'x u8) -> &'x u8 { x }
1417
//~^ ERROR use of undeclared lifetime name
1518
//~^^ ERROR use of undeclared lifetime name

src/test/ui/feature-gate-in_band_lifetimes.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,101 @@
11
error[E0261]: use of undeclared lifetime name `'x`
2-
--> $DIR/feature-gate-in_band_lifetimes.rs:13:12
2+
--> $DIR/feature-gate-in_band_lifetimes.rs:16:12
33
|
44
LL | fn foo(x: &'x u8) -> &'x u8 { x }
55
| ^^ undeclared lifetime
66

77
error[E0261]: use of undeclared lifetime name `'x`
8-
--> $DIR/feature-gate-in_band_lifetimes.rs:13:23
8+
--> $DIR/feature-gate-in_band_lifetimes.rs:16:23
99
|
1010
LL | fn foo(x: &'x u8) -> &'x u8 { x }
1111
| ^^ undeclared lifetime
1212

1313
error[E0261]: use of undeclared lifetime name `'b`
14-
--> $DIR/feature-gate-in_band_lifetimes.rs:25:12
14+
--> $DIR/feature-gate-in_band_lifetimes.rs:28:12
1515
|
1616
LL | impl<'a> X<'b> {
1717
| ^^ undeclared lifetime
1818

1919
error[E0261]: use of undeclared lifetime name `'b`
20-
--> $DIR/feature-gate-in_band_lifetimes.rs:27:27
20+
--> $DIR/feature-gate-in_band_lifetimes.rs:30:27
2121
|
2222
LL | fn inner_2(&self) -> &'b u8 {
2323
| ^^ undeclared lifetime
2424

2525
error[E0261]: use of undeclared lifetime name `'b`
26-
--> $DIR/feature-gate-in_band_lifetimes.rs:33:8
26+
--> $DIR/feature-gate-in_band_lifetimes.rs:36:8
2727
|
2828
LL | impl X<'b> {
2929
| ^^ undeclared lifetime
3030

3131
error[E0261]: use of undeclared lifetime name `'b`
32-
--> $DIR/feature-gate-in_band_lifetimes.rs:35:27
32+
--> $DIR/feature-gate-in_band_lifetimes.rs:38:27
3333
|
3434
LL | fn inner_3(&self) -> &'b u8 {
3535
| ^^ undeclared lifetime
3636

3737
error[E0261]: use of undeclared lifetime name `'a`
38-
--> $DIR/feature-gate-in_band_lifetimes.rs:43:9
38+
--> $DIR/feature-gate-in_band_lifetimes.rs:46:9
3939
|
4040
LL | impl Y<&'a u8> {
4141
| ^^ undeclared lifetime
4242

4343
error[E0261]: use of undeclared lifetime name `'a`
44-
--> $DIR/feature-gate-in_band_lifetimes.rs:45:25
44+
--> $DIR/feature-gate-in_band_lifetimes.rs:48:25
4545
|
4646
LL | fn inner(&self) -> &'a u8 {
4747
| ^^ undeclared lifetime
4848

4949
error[E0261]: use of undeclared lifetime name `'b`
50-
--> $DIR/feature-gate-in_band_lifetimes.rs:53:27
50+
--> $DIR/feature-gate-in_band_lifetimes.rs:56:27
5151
|
5252
LL | fn any_lifetime() -> &'b u8;
5353
| ^^ undeclared lifetime
5454

5555
error[E0261]: use of undeclared lifetime name `'b`
56-
--> $DIR/feature-gate-in_band_lifetimes.rs:55:27
56+
--> $DIR/feature-gate-in_band_lifetimes.rs:58:27
5757
|
5858
LL | fn borrowed_lifetime(&'b self) -> &'b u8;
5959
| ^^ undeclared lifetime
6060

6161
error[E0261]: use of undeclared lifetime name `'b`
62-
--> $DIR/feature-gate-in_band_lifetimes.rs:55:40
62+
--> $DIR/feature-gate-in_band_lifetimes.rs:58:40
6363
|
6464
LL | fn borrowed_lifetime(&'b self) -> &'b u8;
6565
| ^^ undeclared lifetime
6666

6767
error[E0261]: use of undeclared lifetime name `'a`
68-
--> $DIR/feature-gate-in_band_lifetimes.rs:60:14
68+
--> $DIR/feature-gate-in_band_lifetimes.rs:63:14
6969
|
7070
LL | impl MyTrait<'a> for Y<&'a u8> {
7171
| ^^ undeclared lifetime
7272

7373
error[E0261]: use of undeclared lifetime name `'a`
74-
--> $DIR/feature-gate-in_band_lifetimes.rs:60:25
74+
--> $DIR/feature-gate-in_band_lifetimes.rs:63:25
7575
|
7676
LL | impl MyTrait<'a> for Y<&'a u8> {
7777
| ^^ undeclared lifetime
7878

7979
error[E0261]: use of undeclared lifetime name `'a`
80-
--> $DIR/feature-gate-in_band_lifetimes.rs:63:31
80+
--> $DIR/feature-gate-in_band_lifetimes.rs:66:31
8181
|
8282
LL | fn my_lifetime(&self) -> &'a u8 { self.0 }
8383
| ^^ undeclared lifetime
8484

8585
error[E0261]: use of undeclared lifetime name `'b`
86-
--> $DIR/feature-gate-in_band_lifetimes.rs:65:27
86+
--> $DIR/feature-gate-in_band_lifetimes.rs:68:27
8787
|
8888
LL | fn any_lifetime() -> &'b u8 { &0 }
8989
| ^^ undeclared lifetime
9090

9191
error[E0261]: use of undeclared lifetime name `'b`
92-
--> $DIR/feature-gate-in_band_lifetimes.rs:67:27
92+
--> $DIR/feature-gate-in_band_lifetimes.rs:70:27
9393
|
9494
LL | fn borrowed_lifetime(&'b self) -> &'b u8 { &*self.0 }
9595
| ^^ undeclared lifetime
9696

9797
error[E0261]: use of undeclared lifetime name `'b`
98-
--> $DIR/feature-gate-in_band_lifetimes.rs:67:40
98+
--> $DIR/feature-gate-in_band_lifetimes.rs:70:40
9999
|
100100
LL | fn borrowed_lifetime(&'b self) -> &'b u8 { &*self.0 }
101101
| ^^ undeclared lifetime

src/test/ui/in-band-lifetimes/impl/assoc-type.rs renamed to src/test/ui/impl-header-lifetime-elision/assoc-type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#![allow(warnings)]
1515

16-
#![feature(in_band_lifetimes)]
16+
#![feature(impl_header_lifetime_elision)]
1717

1818
trait MyTrait {
1919
type Output;
@@ -30,7 +30,7 @@ impl MyTrait for &u32 {
3030
}
3131

3232
// This is what you have to do:
33-
impl MyTrait for &'a f32 {
33+
impl<'a> MyTrait for &'a f32 {
3434
type Output = &'a f32;
3535
}
3636

src/test/ui/in-band-lifetimes/impl/dyn-trait.rs renamed to src/test/ui/impl-header-lifetime-elision/dyn-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#![allow(warnings)]
1515

16-
#![feature(in_band_lifetimes)]
16+
#![feature(impl_header_lifetime_elision)]
1717

1818
use std::fmt::Debug;
1919

src/test/ui/in-band-lifetimes/impl/path-elided.rs renamed to src/test/ui/impl-header-lifetime-elision/path-elided.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010
#![allow(warnings)]
1111

12-
#![feature(in_band_lifetimes)]
12+
#![feature(impl_header_lifetime_elision)]
1313

1414
trait MyTrait { }
1515

src/test/ui/in-band-lifetimes/impl/path-underscore.rs renamed to src/test/ui/impl-header-lifetime-elision/path-underscore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#![allow(warnings)]
1616

17-
#![feature(in_band_lifetimes)]
17+
#![feature(impl_header_lifetime_elision)]
1818

1919
trait MyTrait { }
2020

src/test/ui/in-band-lifetimes/impl/ref-underscore.rs renamed to src/test/ui/impl-header-lifetime-elision/ref-underscore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#![allow(warnings)]
1616

17-
#![feature(in_band_lifetimes)]
17+
#![feature(impl_header_lifetime_elision)]
1818

1919
trait MyTrait { }
2020

src/test/ui/in-band-lifetimes/impl/trait-elided.rs renamed to src/test/ui/impl-header-lifetime-elision/trait-elided.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010
#![allow(warnings)]
1111

12-
#![feature(in_band_lifetimes)]
12+
#![feature(impl_header_lifetime_elision)]
1313

1414
trait MyTrait<'a> { }
1515

src/test/ui/in-band-lifetimes/impl/trait-underscore.rs renamed to src/test/ui/impl-header-lifetime-elision/trait-underscore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#![allow(warnings)]
1717

18-
#![feature(in_band_lifetimes)]
18+
#![feature(impl_header_lifetime_elision)]
1919

2020
trait MyTrait<'a> { }
2121

0 commit comments

Comments
 (0)