Skip to content

Commit cbcabca

Browse files
author
Alexander Regueiro
committed
Added feature gate.
1 parent 79009ed commit cbcabca

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

src/librustc_resolve/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,8 @@ The `Self` keyword was used outside an impl, trait, or type definition.
775775
Erroneous code example:
776776
777777
```compile_fail,E0411
778-
<Self>::foo; // error: use of `Self` outside of an impl, trait, or type definition
778+
<Self>::foo; // error: use of `Self` outside of an impl, trait, or type
779+
// definition
779780
```
780781
781782
The `Self` keyword represents the current type, which explains why it can only

src/librustc_resolve/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,9 +2204,13 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
22042204
ItemKind::Union(_, ref generics) => {
22052205
self.with_type_parameter_rib(HasTypeParameters(generics, ItemRibKind), |this| {
22062206
let item_def_id = this.definitions.local_def_id(item.id);
2207-
this.with_self_rib(Def::SelfTy(None, Some(item_def_id)), |this| {
2207+
if this.session.features_untracked().self_in_typedefs {
2208+
this.with_self_rib(Def::SelfTy(None, Some(item_def_id)), |this| {
2209+
visit::walk_item(this, item);
2210+
});
2211+
} else {
22082212
visit::walk_item(this, item);
2209-
});
2213+
}
22102214
});
22112215
}
22122216

@@ -2977,7 +2981,12 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
29772981
if is_self_type(path, ns) {
29782982
__diagnostic_used!(E0411);
29792983
err.code(DiagnosticId::Error("E0411".into()));
2980-
err.span_label(span, "`Self` is only available in traits, impls, and type definitions");
2984+
let available_in = if this.session.features_untracked().self_in_typedefs {
2985+
"impls, traits, and type definitions"
2986+
} else {
2987+
"traits and impls"
2988+
};
2989+
err.span_label(span, format!("`Self` is only available in {}", available_in));
29812990
return (err, Vec::new());
29822991
}
29832992
if is_self_value(path, ns) {

src/libsyntax/feature_gate.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ declare_features! (
138138
(active, thread_local, "1.0.0", Some(29594), None),
139139
(active, trace_macros, "1.0.0", Some(29598), None),
140140

141-
// rustc internal, for now:
141+
// rustc internal, for now
142142
(active, intrinsics, "1.0.0", None, None),
143143
(active, lang_items, "1.0.0", None, None),
144144
(active, format_args_nl, "1.29.0", None, None),
@@ -168,6 +168,7 @@ declare_features! (
168168
(active, optin_builtin_traits, "1.0.0", Some(13231), None),
169169

170170
// Allows use of #[staged_api]
171+
//
171172
// rustc internal
172173
(active, staged_api, "1.0.0", None, None),
173174

@@ -245,24 +246,25 @@ declare_features! (
245246
// Allows associated type defaults
246247
(active, associated_type_defaults, "1.2.0", Some(29661), None),
247248

248-
// allow `repr(simd)`, and importing the various simd intrinsics
249+
// Allows `repr(simd)`, and importing the various simd intrinsics
249250
(active, repr_simd, "1.4.0", Some(27731), None),
250251

251-
// allow `extern "platform-intrinsic" { ... }`
252+
// Allows `extern "platform-intrinsic" { ... }`
252253
(active, platform_intrinsics, "1.4.0", Some(27731), None),
253254

254-
// allow `#[unwind(..)]`
255+
// Allows `#[unwind(..)]`
255256
// rustc internal for rust runtime
256257
(active, unwind_attributes, "1.4.0", None, None),
257258

258-
// allow the use of `#[naked]` on functions.
259+
// Allows the use of `#[naked]` on functions.
259260
(active, naked_functions, "1.9.0", Some(32408), None),
260261

261-
// allow `#[no_debug]`
262+
// Allows `#[no_debug]`
262263
(active, no_debug, "1.5.0", Some(29721), None),
263264

264-
// allow `#[omit_gdb_pretty_printer_section]`
265-
// rustc internal.
265+
// Allows `#[omit_gdb_pretty_printer_section]`
266+
//
267+
// rustc internal
266268
(active, omit_gdb_pretty_printer_section, "1.5.0", None, None),
267269

268270
// Allows cfg(target_vendor = "...").
@@ -292,10 +294,10 @@ declare_features! (
292294
// The `!` type. Does not imply exhaustive_patterns (below) any more.
293295
(active, never_type, "1.13.0", Some(35121), None),
294296

295-
// Allows exhaustive pattern matching on types that contain uninhabited types.
297+
// Allows exhaustive pattern matching on types that contain uninhabited types
296298
(active, exhaustive_patterns, "1.13.0", Some(51085), None),
297299

298-
// Allows all literals in attribute lists and values of key-value pairs.
300+
// Allows all literals in attribute lists and values of key-value pairs
299301
(active, attr_literals, "1.13.0", Some(34981), None),
300302

301303
// Allows untagged unions `union U { ... }`
@@ -334,6 +336,7 @@ declare_features! (
334336
(active, sanitizer_runtime, "1.17.0", None, None),
335337

336338
// Used to identify crates that contain the profiler runtime
339+
//
337340
// rustc internal
338341
(active, profiler_runtime, "1.18.0", None, None),
339342

@@ -391,7 +394,7 @@ declare_features! (
391394
// extern types
392395
(active, extern_types, "1.23.0", Some(43467), None),
393396

394-
// Allow trait methods with arbitrary self types
397+
// Allows trait methods with arbitrary self types
395398
(active, arbitrary_self_types, "1.23.0", Some(44874), None),
396399

397400
// `crate` in paths
@@ -400,7 +403,7 @@ declare_features! (
400403
// In-band lifetime bindings (e.g. `fn foo(x: &'a u8) -> &'a u8`)
401404
(active, in_band_lifetimes, "1.23.0", Some(44524), None),
402405

403-
// generic associated types (RFC 1598)
406+
// Generic associated types (RFC 1598)
404407
(active, generic_associated_types, "1.23.0", Some(44265), None),
405408

406409
// Resolve absolute paths as paths from other crates
@@ -475,7 +478,7 @@ declare_features! (
475478
// Scoped lints
476479
(active, tool_lints, "1.28.0", Some(44690), None),
477480

478-
// allow irrefutable patterns in if-let and while-let statements (RFC 2086)
481+
// Allows irrefutable patterns in if-let and while-let statements (RFC 2086)
479482
(active, irrefutable_let_patterns, "1.27.0", Some(44495), None),
480483

481484
// Allows use of the :literal macro fragment specifier (RFC 1576)
@@ -505,11 +508,14 @@ declare_features! (
505508
// impl Debug for Foo<'_>
506509
(active, impl_header_lifetime_elision, "1.30.0", Some(15872), Some(Edition::Edition2018)),
507510

508-
// Support for arbitrary delimited token streams in non-macro attributes.
511+
// Support for arbitrary delimited token streams in non-macro attributes
509512
(active, unrestricted_attribute_tokens, "1.30.0", Some(44690), None),
510513

511-
// Allows `use x::y;` to resolve through `self::x`, not just `::x`.
514+
// Allows `use x::y;` to resolve through `self::x`, not just `::x`
512515
(active, uniform_paths, "1.30.0", Some(53130), None),
516+
517+
// Allows `Self` in type definitions
518+
(active, self_in_typedefs, "1.30.0", Some(49303), None),
513519
);
514520

515521
declare_features! (
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
11+
enum StackList<'a, T: 'a> {
12+
Nil,
13+
Cons(T, &'a Self)
14+
//~^ ERROR cannot find type `Self` in this scope
15+
//~| `Self` is only available in traits and impls
16+
}
17+
18+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0411]: cannot find type `Self` in this scope
2+
--> $DIR/feature-gate-self-in-typedefs.rs:13:17
3+
|
4+
LL | Cons(T, &'a Self)
5+
| ^^^^ `Self` is only available in traits and impls
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0411`.

0 commit comments

Comments
 (0)