Skip to content

Commit 18597d8

Browse files
committed
Temporarily allow const impl and impl const at the same time to migrate
1 parent 427deb6 commit 18597d8

File tree

7 files changed

+40
-89
lines changed

7 files changed

+40
-89
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,7 @@ impl<'a> Parser<'a> {
559559
attrs: &mut AttrVec,
560560
defaultness: Defaultness,
561561
) -> PResult<'a, ItemKind> {
562-
if self.eat_keyword(exp!(Const)) {
563-
return self.recover_const_impl(self.prev_token.span, attrs, defaultness);
564-
}
562+
let mut constness = self.parse_constness(Case::Sensitive);
565563
let safety = self.parse_safety(Case::Sensitive);
566564
self.expect_keyword(exp!(Impl))?;
567565

@@ -576,7 +574,11 @@ impl<'a> Parser<'a> {
576574
generics
577575
};
578576

579-
let constness = self.parse_constness(Case::Sensitive);
577+
if let Const::No = constness {
578+
// FIXME(const_trait_impl): disallow `impl const Trait`
579+
constness = self.parse_constness(Case::Sensitive);
580+
}
581+
580582
if let Const::Yes(span) = constness {
581583
self.psess.gated_spans.gate(sym::const_trait_impl, span);
582584
}
@@ -1345,46 +1347,6 @@ impl<'a> Parser<'a> {
13451347
}
13461348
}
13471349

1348-
/// Recover on `const impl` with `const` already eaten.
1349-
fn recover_const_impl(
1350-
&mut self,
1351-
const_span: Span,
1352-
attrs: &mut AttrVec,
1353-
defaultness: Defaultness,
1354-
) -> PResult<'a, ItemKind> {
1355-
let impl_span = self.token.span;
1356-
let err = self.expected_ident_found_err();
1357-
1358-
// Only try to recover if this is implementing a trait for a type
1359-
let mut item_kind = match self.parse_item_impl(attrs, defaultness) {
1360-
Ok(item_kind) => item_kind,
1361-
Err(recovery_error) => {
1362-
// Recovery failed, raise the "expected identifier" error
1363-
recovery_error.cancel();
1364-
return Err(err);
1365-
}
1366-
};
1367-
1368-
match &mut item_kind {
1369-
ItemKind::Impl(Impl { of_trait: Some(of_trait), .. }) => {
1370-
of_trait.constness = Const::Yes(const_span);
1371-
1372-
let before_trait = of_trait.trait_ref.path.span.shrink_to_lo();
1373-
let const_up_to_impl = const_span.with_hi(impl_span.lo());
1374-
err.with_multipart_suggestion(
1375-
"you might have meant to write a const trait impl",
1376-
vec![(const_up_to_impl, "".to_owned()), (before_trait, "const ".to_owned())],
1377-
Applicability::MaybeIncorrect,
1378-
)
1379-
.emit();
1380-
}
1381-
ItemKind::Impl { .. } => return Err(err),
1382-
_ => unreachable!(),
1383-
}
1384-
1385-
Ok(item_kind)
1386-
}
1387-
13881350
/// Parse a static item with the prefix `"static" "mut"?` already parsed and stored in
13891351
/// `mutability`.
13901352
///
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
trait T { const
2-
impl //~ ERROR: expected identifier, found keyword `impl`
3-
}
1+
trait T {
2+
const //~ ERROR: const trait impls are experimental
3+
impl
4+
} //~ ERROR: expected type, found `}`
45

56
fn main() {}
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
error: expected identifier, found keyword `impl`
2-
--> $DIR/issue-81806.rs:2:1
1+
error: expected type, found `}`
2+
--> $DIR/issue-81806.rs:4:1
33
|
4-
LL | trait T { const
4+
LL | trait T {
55
| - while parsing this item list starting here
6-
LL | impl
7-
| ^^^^ expected identifier, found keyword
6+
...
87
LL | }
9-
| - the item list ends here
8+
| ^
9+
| |
10+
| expected type
11+
| the item list ends here
12+
13+
error[E0658]: const trait impls are experimental
14+
--> $DIR/issue-81806.rs:2:1
1015
|
11-
help: escape `impl` to use it as an identifier
16+
LL | const
17+
| ^^^^^
1218
|
13-
LL | r#impl
14-
| ++
19+
= note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
20+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
21+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1522

16-
error: aborting due to 1 previous error
23+
error: aborting due to 2 previous errors
1724

25+
For more information about this error, try `rustc --explain E0658`.

tests/ui/traits/const-traits/const-impl-norecover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
struct Foo;
44

5-
const impl Foo { //~ ERROR: expected identifier, found keyword
5+
const impl Foo { //~ ERROR: inherent impls cannot be const
66
fn bar() {}
77
}
88

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
error: expected identifier, found keyword `impl`
2-
--> $DIR/const-impl-norecover.rs:5:7
1+
error: inherent impls cannot be const
2+
--> $DIR/const-impl-norecover.rs:5:12
33
|
44
LL | const impl Foo {
5-
| ^^^^ expected identifier, found keyword
5+
| ----- ^^^ inherent impl for this type
6+
| |
7+
| const because of this
8+
|
9+
= note: only trait implementations may be annotated with `const`
610

711
error: aborting due to 1 previous error
812

tests/ui/traits/const-traits/const-impl-recovery.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#![feature(const_trait_impl)]
22

3+
//@ check-pass
4+
35
#[const_trait]
46
trait Foo {}
57

6-
const impl Foo for i32 {} //~ ERROR: expected identifier, found keyword
8+
const impl Foo for i32 {}
79

810
#[const_trait]
911
trait Bar {}
1012

11-
const impl<T: Foo> Bar for T {} //~ ERROR: expected identifier, found keyword
13+
const impl<T: Foo> Bar for T {}
1214

1315
const fn still_implements<T: Bar>() {}
1416

tests/ui/traits/const-traits/const-impl-recovery.stderr

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)