Skip to content

Commit 4beea17

Browse files
committed
Deduplicate code for path suggestion
1 parent 5390414 commit 4beea17

File tree

3 files changed

+43
-64
lines changed

3 files changed

+43
-64
lines changed

src/librustc_resolve/error_reporting.rs

+41-62
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use log::debug;
55
use rustc::hir::def::{Def, CtorKind, Namespace::*};
66
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
77
use rustc::session::config::nightly_options;
8-
use syntax::ast::{ExprKind};
8+
use syntax::ast::{Expr, ExprKind};
99
use syntax::symbol::keywords;
1010
use syntax_pos::Span;
1111

@@ -250,6 +250,29 @@ impl<'a> Resolver<'a> {
250250
let ns = source.namespace();
251251
let is_expected = &|def| source.is_expected(def);
252252

253+
let path_sep = |err: &mut DiagnosticBuilder<'_>, expr: &Expr| match expr.node {
254+
ExprKind::Field(_, ident) => {
255+
err.span_suggestion(
256+
expr.span,
257+
"use the path separator to refer to an item",
258+
format!("{}::{}", path_str, ident),
259+
Applicability::MaybeIncorrect,
260+
);
261+
true
262+
}
263+
ExprKind::MethodCall(ref segment, ..) => {
264+
let span = expr.span.with_hi(segment.ident.span.hi());
265+
err.span_suggestion(
266+
span,
267+
"use the path separator to refer to an item",
268+
format!("{}::{}", path_str, segment.ident),
269+
Applicability::MaybeIncorrect,
270+
);
271+
true
272+
}
273+
_ => false,
274+
};
275+
253276
match (def, source) {
254277
(Def::Macro(..), _) => {
255278
err.span_suggestion(
@@ -259,8 +282,7 @@ impl<'a> Resolver<'a> {
259282
Applicability::MaybeIncorrect,
260283
);
261284
if path_str == "try" && span.rust_2015() {
262-
err.note("if you want the `try` keyword, \
263-
you need to be in the 2018 edition");
285+
err.note("if you want the `try` keyword, you need to be in the 2018 edition");
264286
}
265287
}
266288
(Def::TyAlias(..), PathSource::Trait(_)) => {
@@ -269,25 +291,8 @@ impl<'a> Resolver<'a> {
269291
err.note("did you mean to use a trait alias?");
270292
}
271293
}
272-
(Def::Mod(..), PathSource::Expr(Some(parent))) => match parent.node {
273-
ExprKind::Field(_, ident) => {
274-
err.span_suggestion(
275-
parent.span,
276-
"use the path separator to refer to an item",
277-
format!("{}::{}", path_str, ident),
278-
Applicability::MaybeIncorrect,
279-
);
280-
}
281-
ExprKind::MethodCall(ref segment, ..) => {
282-
let span = parent.span.with_hi(segment.ident.span.hi());
283-
err.span_suggestion(
284-
span,
285-
"use the path separator to refer to an item",
286-
format!("{}::{}", path_str, segment.ident),
287-
Applicability::MaybeIncorrect,
288-
);
289-
}
290-
_ => return false,
294+
(Def::Mod(..), PathSource::Expr(Some(parent))) => if !path_sep(err, &parent) {
295+
return false;
291296
},
292297
(Def::Enum(..), PathSource::TupleStruct)
293298
| (Def::Enum(..), PathSource::Expr(..)) => {
@@ -315,8 +320,10 @@ impl<'a> Resolver<'a> {
315320
= self.struct_constructors.get(&def_id).cloned() {
316321
let accessible_ctor = self.is_accessible(ctor_vis);
317322
if is_expected(ctor_def) && !accessible_ctor {
318-
err.span_label(span, format!("constructor is not visible \
319-
here due to private fields"));
323+
err.span_label(
324+
span,
325+
format!("constructor is not visible here due to private fields"),
326+
);
320327
}
321328
} else {
322329
// HACK(estebank): find a better way to figure out that this was a
@@ -366,36 +373,12 @@ impl<'a> Resolver<'a> {
366373
}
367374
}
368375
match source {
369-
PathSource::Expr(Some(parent)) => {
370-
match parent.node {
371-
ExprKind::MethodCall(ref path_assignment, _) => {
372-
err.span_suggestion(
373-
sm.start_point(parent.span)
374-
.to(path_assignment.ident.span),
375-
"use `::` to access an associated function",
376-
format!("{}::{}",
377-
path_str,
378-
path_assignment.ident),
379-
Applicability::MaybeIncorrect
380-
);
381-
},
382-
ExprKind::Field(ref _expr, ident) => {
383-
err.span_suggestion(
384-
sm.start_point(parent.span).to(ident.span),
385-
"use `::` to access an associated item",
386-
format!("{}::{}", path_str, ident),
387-
Applicability::MaybeIncorrect
388-
);
389-
}
390-
_ => {
391-
err.span_label(
392-
span,
393-
format!("did you mean `{} {{ /* fields */ }}`?",
394-
path_str),
395-
);
396-
},
397-
}
398-
},
376+
PathSource::Expr(Some(parent)) => if !path_sep(err, &parent) {
377+
err.span_label(
378+
span,
379+
format!("did you mean `{} {{ /* fields */ }}`?", path_str),
380+
);
381+
}
399382
PathSource::Expr(None) if followed_by_brace == true => {
400383
if let Some((sp, snippet)) = closing_brace {
401384
err.span_suggestion(
@@ -407,16 +390,14 @@ impl<'a> Resolver<'a> {
407390
} else {
408391
err.span_label(
409392
span,
410-
format!("did you mean `({} {{ /* fields */ }})`?",
411-
path_str),
393+
format!("did you mean `({} {{ /* fields */ }})`?", path_str),
412394
);
413395
}
414396
},
415397
_ => {
416398
err.span_label(
417399
span,
418-
format!("did you mean `{} {{ /* fields */ }}`?",
419-
path_str),
400+
format!("did you mean `{} {{ /* fields */ }}`?", path_str),
420401
);
421402
},
422403
}
@@ -425,13 +406,11 @@ impl<'a> Resolver<'a> {
425406
(Def::Union(..), _) |
426407
(Def::Variant(..), _) |
427408
(Def::Ctor(_, _, CtorKind::Fictive), _) if ns == ValueNS => {
428-
err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?",
429-
path_str));
409+
err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?", path_str));
430410
}
431411
(Def::SelfTy(..), _) if ns == ValueNS => {
432412
err.span_label(span, fallback_label);
433-
err.note("can't use `Self` as a constructor, you must use the \
434-
implemented struct");
413+
err.note("can't use `Self` as a constructor, you must use the implemented struct");
435414
}
436415
(Def::TyAlias(_), _) | (Def::AssociatedTy(..), _) if ns == ValueNS => {
437416
err.note("can't use a type alias as a constructor");

src/test/ui/resolve/issue-22692.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0423]: expected value, found struct `String`
44
LL | let _ = String.new();
55
| ^^^^^^----
66
| |
7-
| help: use `::` to access an associated function: `String::new`
7+
| help: use the path separator to refer to an item: `String::new`
88

99
error: aborting due to previous error
1010

src/test/ui/suggestions/assoc-const-as-field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0423]: expected value, found struct `Mod::Foo`
44
LL | foo(Mod::Foo.Bar);
55
| ^^^^^^^^----
66
| |
7-
| help: use `::` to access an associated item: `Mod::Foo::Bar`
7+
| help: use the path separator to refer to an item: `Mod::Foo::Bar`
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)