Skip to content

Commit 28228bb

Browse files
committed
Remove duplicated : -> :: suggestion from parse error
1 parent 5bdcaf6 commit 28228bb

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

src/librustc_resolve/late/diagnostics.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_span::symbol::{kw, sym, Ident};
2020
use rustc_span::Span;
2121

2222
use log::debug;
23+
use std::ops::Deref;
2324

2425
type Res = def::Res<ast::NodeId>;
2526

@@ -614,6 +615,53 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
614615
return false;
615616
}
616617
}
618+
(
619+
Res::Def(DefKind::Enum, def_id),
620+
PathSource::Expr(Some(Expr { kind: ExprKind::Type(expr, ty), .. })),
621+
) => {
622+
match (expr.deref(), ty.deref()) {
623+
(
624+
Expr { kind: ExprKind::Path(..), .. },
625+
Ty { kind: TyKind::Path(None, path), .. },
626+
) if path.segments.len() == 1 => {
627+
if let Some(variants) = self.collect_enum_variants(def_id) {
628+
if variants
629+
.into_iter()
630+
.filter(|variant| {
631+
variant.segments[variant.segments.len() - 1].ident
632+
== path.segments[0].ident
633+
})
634+
.next()
635+
.is_some()
636+
{
637+
err.delay_as_bug();
638+
let sp = expr.span.between(ty.span);
639+
if self
640+
.r
641+
.session
642+
.parse_sess
643+
.type_ascription_path_suggestions
644+
.borrow()
645+
.contains(&sp)
646+
{
647+
// We already suggested changing `:` into `::` during parsing.
648+
return false;
649+
}
650+
let mut err =
651+
self.r.session.struct_span_err(sp, "expected `::`, found `:`");
652+
err.span_suggestion(
653+
sp,
654+
"write a path separator instead",
655+
"::".to_string(),
656+
Applicability::MachineApplicable,
657+
);
658+
err.emit();
659+
}
660+
}
661+
}
662+
_ => {}
663+
}
664+
}
617665
(
618666
Res::Def(DefKind::Enum, def_id),
619667
PathSource::TupleStruct(_) | PathSource::Expr(..),
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fn main() {
22
let _ = Option:Some(vec![0, 1]); //~ ERROR expected type, found
3-
//~^ ERROR expected value, found enum `Option`
43
}
54

65
// This case isn't currently being handled gracefully due to the macro invocation.

src/test/ui/type/ascription/issue-47666.stderr

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,5 @@ LL | let _ = Option:Some(vec![0, 1]);
1212
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
1313
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1414

15-
error[E0423]: expected value, found enum `Option`
16-
--> $DIR/issue-47666.rs:2:13
17-
|
18-
LL | let _ = Option:Some(vec![0, 1]);
19-
| ^^^^^^
20-
|
21-
help: try using one of the enum's variants
22-
|
23-
LL | let _ = std::option::Option::None:Some(vec![0, 1]);
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
25-
LL | let _ = std::option::Option::Some:Some(vec![0, 1]);
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
27-
28-
error: aborting due to 2 previous errors
15+
error: aborting due to previous error
2916

30-
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)