Skip to content

Commit 74fa315

Browse files
committed
Place impl restriction behind feature gate
1 parent eb20fdf commit 74fa315

File tree

7 files changed

+57
-6
lines changed

7 files changed

+57
-6
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
515515
gate_all!(where_clause_attrs, "attributes in `where` clause are unstable");
516516
gate_all!(super_let, "`super let` is experimental");
517517
gate_all!(frontmatter, "frontmatters are experimental");
518+
gate_all!(impl_restriction, "impl restrictions are experimental");
518519

519520
if !visitor.features.never_patterns() {
520521
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ declare_features! (
532532
(unstable, half_open_range_patterns_in_slices, "1.66.0", Some(67264)),
533533
/// Allows `if let` guard in match arms.
534534
(unstable, if_let_guard, "1.47.0", Some(51114)),
535+
/// Allows `impl(crate) trait Foo` restrictions
536+
(unstable, impl_restriction, "CURRENT_RUSTC_VERSION", Some(105077)),
535537
/// Allows `impl Trait` to be used inside associated types (RFC 2515).
536538
(unstable, impl_trait_in_assoc_type, "1.70.0", Some(63063)),
537539
/// Allows `impl Trait` in bindings (`let`).

compiler/rustc_parse/src/parser/item.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,13 @@ impl<'a> Parser<'a> {
925925

926926
/// Parses `[ impl(in path) ]? unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
927927
fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemKind> {
928-
let impl_restriction =
929-
self.parse_restriction(exp!(Impl), "implementable", "impl", FollowedByType::No)?;
928+
let impl_restriction = self.parse_restriction(
929+
exp!(Impl),
930+
Some(sym::impl_restriction),
931+
"implementable",
932+
"impl",
933+
FollowedByType::No,
934+
)?;
930935
let safety = self.parse_safety(Case::Sensitive);
931936
// Parse optional `auto` prefix.
932937
let is_auto = if self.eat_keyword(exp!(Auto)) {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,7 @@ impl<'a> Parser<'a> {
15201520
fn parse_restriction(
15211521
&mut self,
15221522
kw: ExpKeywordPair,
1523+
feature_gate: Option<Symbol>,
15231524
action: &'static str,
15241525
description: &'static str,
15251526
fbt: FollowedByType,
@@ -1531,6 +1532,13 @@ impl<'a> Parser<'a> {
15311532
return Ok(Restriction::implied().with_span(self.token.span.shrink_to_lo()));
15321533
}
15331534

1535+
let gate = |span| {
1536+
if let Some(feature_gate) = feature_gate {
1537+
self.psess.gated_spans.gate(feature_gate, span);
1538+
}
1539+
span
1540+
};
1541+
15341542
let lo = self.prev_token.span;
15351543

15361544
if self.check(exp!(OpenParen)) {
@@ -1545,16 +1553,16 @@ impl<'a> Parser<'a> {
15451553
let path = self.parse_path(PathStyle::Mod)?; // `path`
15461554
self.expect(exp!(CloseParen))?; // `)`
15471555
return Ok(Restriction::restricted(P(path), ast::DUMMY_NODE_ID, false)
1548-
.with_span(lo.to(self.prev_token.span)));
1556+
.with_span(gate(lo.to(self.prev_token.span))));
15491557
} else if self.look_ahead(2, |t| t == &TokenKind::CloseParen)
15501558
&& self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
15511559
{
15521560
// Parse `kw(crate)`, `kw(self)`, or `kw(super)`.
15531561
self.bump(); // `(`
15541562
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
15551563
self.expect(exp!(CloseParen))?; // `)`
1556-
return Ok(Restriction::restricted(P(path), ast::DUMMY_NODE_ID, false)
1557-
.with_span(lo.to(self.prev_token.span)));
1564+
return Ok(Restriction::restricted(P(path), ast::DUMMY_NODE_ID, true)
1565+
.with_span(gate(lo.to(self.prev_token.span))));
15581566
} else if let FollowedByType::No = fbt {
15591567
// Provide this diagnostic if a type cannot follow;
15601568
// in particular, if this is not a tuple struct.
@@ -1563,7 +1571,7 @@ impl<'a> Parser<'a> {
15631571
}
15641572
}
15651573

1566-
Ok(Restriction::unrestricted().with_span(lo))
1574+
Ok(Restriction::unrestricted().with_span(gate(lo)))
15671575
}
15681576

15691577
/// Recovery for e.g. `kw(something) fn ...` or `struct X { kw(something) y: Z }`

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ symbols! {
11501150
ignore,
11511151
impl_header_lifetime_elision,
11521152
impl_lint_pass,
1153+
impl_restriction,
11531154
impl_trait_in_assoc_type,
11541155
impl_trait_in_bindings,
11551156
impl_trait_in_fn_trait_return,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: --crate-type=lib
2+
//@ revisions: with_gate without_gate
3+
//@[with_gate] check-pass
4+
5+
#![cfg_attr(with_gate, feature(impl_restriction))]
6+
7+
pub impl(crate) trait Bar {} //[without_gate]~ ERROR
8+
9+
mod foo {
10+
pub impl(in crate::foo) trait Baz {} //[without_gate]~ ERROR
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0658]: impl restrictions are experimental
2+
--> $DIR/feature-gate-impl-restriction.rs:7:5
3+
|
4+
LL | pub impl(crate) trait Bar {}
5+
| ^^^^^^^^^^^
6+
|
7+
= note: see issue #105077 <https://github.com/rust-lang/rust/issues/105077> for more information
8+
= help: add `#![feature(impl_restriction)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: impl restrictions are experimental
12+
--> $DIR/feature-gate-impl-restriction.rs:10:9
13+
|
14+
LL | pub impl(in crate::foo) trait Baz {}
15+
| ^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #105077 <https://github.com/rust-lang/rust/issues/105077> for more information
18+
= help: add `#![feature(impl_restriction)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)