Skip to content

Commit 1078803

Browse files
committed
Place impl restriction behind feature gate
1 parent 6e3e840 commit 1078803

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
@@ -534,6 +534,8 @@ declare_features! (
534534
(unstable, half_open_range_patterns_in_slices, "1.66.0", Some(67264)),
535535
/// Allows `if let` guard in match arms.
536536
(unstable, if_let_guard, "1.47.0", Some(51114)),
537+
/// Allows `impl(crate) trait Foo` restrictions
538+
(unstable, impl_restriction, "CURRENT_RUSTC_VERSION", Some(105077)),
537539
/// Allows `impl Trait` to be used inside associated types (RFC 2515).
538540
(unstable, impl_trait_in_assoc_type, "1.70.0", Some(63063)),
539541
/// 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
@@ -1521,6 +1521,7 @@ impl<'a> Parser<'a> {
15211521
fn parse_restriction(
15221522
&mut self,
15231523
kw: ExpKeywordPair,
1524+
feature_gate: Option<Symbol>,
15241525
action: &'static str,
15251526
description: &'static str,
15261527
fbt: FollowedByType,
@@ -1532,6 +1533,13 @@ impl<'a> Parser<'a> {
15321533
return Ok(Restriction::implied().with_span(self.token.span.shrink_to_lo()));
15331534
}
15341535

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

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

1567-
Ok(Restriction::unrestricted().with_span(lo))
1575+
Ok(Restriction::unrestricted().with_span(gate(lo)))
15681576
}
15691577

15701578
/// 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
@@ -1154,6 +1154,7 @@ symbols! {
11541154
ignore,
11551155
impl_header_lifetime_elision,
11561156
impl_lint_pass,
1157+
impl_restriction,
11571158
impl_trait_in_assoc_type,
11581159
impl_trait_in_bindings,
11591160
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)