Skip to content

Commit 55e8df2

Browse files
committed
Auto merge of rust-lang#113532 - matthiaskrgr:rollup-mty7u37, r=matthiaskrgr
Rollup of 2 pull requests Successful merges: - rust-lang#113331 (Add filter with following segment while lookup typo for path) - rust-lang#113524 (Remove the library/term exception in tidy's pal checker code) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a482149 + 34aebbd commit 55e8df2

15 files changed

+301
-47
lines changed

compiler/rustc_resolve/src/late.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3503,7 +3503,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
35033503
let report_errors = |this: &mut Self, res: Option<Res>| {
35043504
if this.should_report_errs() {
35053505
let (err, candidates) =
3506-
this.smart_resolve_report_errors(path, path, path_span, source, res);
3506+
this.smart_resolve_report_errors(path, None, path_span, source, res);
35073507

35083508
let def_id = this.parent_scope.module.nearest_parent_mod();
35093509
let instead = res.is_some();
@@ -3555,14 +3555,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
35553555
// Before we start looking for candidates, we have to get our hands
35563556
// on the type user is trying to perform invocation on; basically:
35573557
// we're transforming `HashMap::new` into just `HashMap`.
3558-
let prefix_path = match path.split_last() {
3559-
Some((_, path)) if !path.is_empty() => path,
3558+
let (following_seg, prefix_path) = match path.split_last() {
3559+
Some((last, path)) if !path.is_empty() => (Some(last), path),
35603560
_ => return Some(parent_err),
35613561
};
35623562

35633563
let (mut err, candidates) = this.smart_resolve_report_errors(
35643564
prefix_path,
3565-
path,
3565+
following_seg,
35663566
path_span,
35673567
PathSource::Type,
35683568
None,

compiler/rustc_resolve/src/late/diagnostics.rs

+47-19
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
332332
pub(crate) fn smart_resolve_partial_mod_path_errors(
333333
&mut self,
334334
prefix_path: &[Segment],
335-
path: &[Segment],
335+
following_seg: Option<&Segment>,
336336
) -> Vec<ImportSuggestion> {
337-
let next_seg = if path.len() >= prefix_path.len() + 1 && prefix_path.len() == 1 {
338-
path.get(prefix_path.len())
339-
} else {
340-
None
341-
};
342337
if let Some(segment) = prefix_path.last() &&
343-
let Some(next_seg) = next_seg {
338+
let Some(following_seg) = following_seg
339+
{
344340
let candidates = self.r.lookup_import_candidates(
345341
segment.ident,
346342
Namespace::TypeNS,
@@ -353,9 +349,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
353349
.filter(|candidate| {
354350
if let Some(def_id) = candidate.did &&
355351
let Some(module) = self.r.get_module(def_id) {
356-
self.r.resolutions(module).borrow().iter().any(|(key, _r)| {
357-
key.ident.name == next_seg.ident.name
358-
})
352+
Some(def_id) != self.parent_scope.module.opt_def_id() &&
353+
self.r.resolutions(module).borrow().iter().any(|(key, _r)| {
354+
key.ident.name == following_seg.ident.name
355+
})
359356
} else {
360357
false
361358
}
@@ -371,7 +368,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
371368
pub(crate) fn smart_resolve_report_errors(
372369
&mut self,
373370
path: &[Segment],
374-
full_path: &[Segment],
371+
following_seg: Option<&Segment>,
375372
span: Span,
376373
source: PathSource<'_>,
377374
res: Option<Res>,
@@ -412,8 +409,15 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
412409
return (err, Vec::new());
413410
}
414411

415-
let (found, candidates) =
416-
self.try_lookup_name_relaxed(&mut err, source, path, full_path, span, res, &base_error);
412+
let (found, candidates) = self.try_lookup_name_relaxed(
413+
&mut err,
414+
source,
415+
path,
416+
following_seg,
417+
span,
418+
res,
419+
&base_error,
420+
);
417421
if found {
418422
return (err, candidates);
419423
}
@@ -422,7 +426,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
422426

423427
// if we have suggested using pattern matching, then don't add needless suggestions
424428
// for typos.
425-
fallback |= self.suggest_typo(&mut err, source, path, span, &base_error);
429+
fallback |= self.suggest_typo(&mut err, source, path, following_seg, span, &base_error);
426430

427431
if fallback {
428432
// Fallback label.
@@ -519,7 +523,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
519523
err: &mut Diagnostic,
520524
source: PathSource<'_>,
521525
path: &[Segment],
522-
full_path: &[Segment],
526+
following_seg: Option<&Segment>,
523527
span: Span,
524528
res: Option<Res>,
525529
base_error: &BaseError,
@@ -590,8 +594,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
590594
}
591595

592596
// Try finding a suitable replacement.
593-
let typo_sugg =
594-
self.lookup_typo_candidate(path, source.namespace(), is_expected).to_opt_suggestion();
597+
let typo_sugg = self
598+
.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected)
599+
.to_opt_suggestion();
595600
if path.len() == 1 && self.self_type_is_available() {
596601
if let Some(candidate) =
597602
self.lookup_assoc_candidate(ident, ns, is_expected, source.is_call())
@@ -690,7 +695,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
690695
}
691696

692697
if candidates.is_empty() {
693-
candidates = self.smart_resolve_partial_mod_path_errors(path, full_path);
698+
candidates = self.smart_resolve_partial_mod_path_errors(path, following_seg);
694699
}
695700

696701
return (false, candidates);
@@ -776,12 +781,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
776781
err: &mut Diagnostic,
777782
source: PathSource<'_>,
778783
path: &[Segment],
784+
following_seg: Option<&Segment>,
779785
span: Span,
780786
base_error: &BaseError,
781787
) -> bool {
782788
let is_expected = &|res| source.is_expected(res);
783789
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
784-
let typo_sugg = self.lookup_typo_candidate(path, source.namespace(), is_expected);
790+
let typo_sugg =
791+
self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
785792
let is_in_same_file = &|sp1, sp2| {
786793
let source_map = self.r.tcx.sess.source_map();
787794
let file1 = source_map.span_to_filename(sp1);
@@ -1715,6 +1722,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17151722
fn lookup_typo_candidate(
17161723
&mut self,
17171724
path: &[Segment],
1725+
following_seg: Option<&Segment>,
17181726
ns: Namespace,
17191727
filter_fn: &impl Fn(Res) -> bool,
17201728
) -> TypoCandidate {
@@ -1793,6 +1801,26 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17931801
}
17941802
}
17951803

1804+
// if next_seg is present, let's filter everything that does not continue the path
1805+
if let Some(following_seg) = following_seg {
1806+
names.retain(|suggestion| match suggestion.res {
1807+
Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _) => {
1808+
// FIXME: this is not totally accurate, but mostly works
1809+
suggestion.candidate != following_seg.ident.name
1810+
}
1811+
Res::Def(DefKind::Mod, def_id) => self.r.get_module(def_id).map_or_else(
1812+
|| false,
1813+
|module| {
1814+
self.r
1815+
.resolutions(module)
1816+
.borrow()
1817+
.iter()
1818+
.any(|(key, _)| key.ident.name == following_seg.ident.name)
1819+
},
1820+
),
1821+
_ => true,
1822+
});
1823+
}
17961824
let name = path[path.len() - 1].ident.name;
17971825
// Make sure error reporting is deterministic.
17981826
names.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));

src/tools/tidy/src/pal.rs

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const EXCEPTION_PATHS: &[&str] = &[
3939
"library/panic_unwind",
4040
"library/unwind",
4141
"library/rtstartup", // Not sure what to do about this. magic stuff for mingw
42-
"library/term", // Not sure how to make this crate portable, but test crate needs it.
4342
"library/test", // Probably should defer to unstable `std::sys` APIs.
4443
// The `VaList` implementation must have platform specific code.
4544
// The Windows implementation of a `va_list` is always a character

tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ LL | let _: u8 = ::core::default::Default();
1313
| ^^^^ maybe a missing crate `core`?
1414
|
1515
= help: consider adding `extern crate core` to use the `core` crate
16+
help: consider importing this module
17+
|
18+
LL + use std::default;
19+
|
20+
help: if you import `default`, refer to it directly
21+
|
22+
LL - let _: u8 = ::core::default::Default();
23+
LL + let _: u8 = default::Default();
24+
|
1625

1726
error: aborting due to 2 previous errors
1827

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// edition:2018
2+
#![feature(decl_macro)]
3+
4+
macro a() {
5+
extern crate core as my_core;
6+
mod v {
7+
// Early resolution.
8+
use my_core; //~ ERROR unresolved import `my_core`
9+
}
10+
mod u {
11+
// Late resolution.
12+
fn f() { my_core::mem::drop(0); }
13+
//~^ ERROR failed to resolve: use of undeclared crate or module `my_core`
14+
}
15+
}
16+
17+
a!();
18+
19+
mod v {
20+
// Early resolution.
21+
use my_core; //~ ERROR unresolved import `my_core`
22+
}
23+
mod u {
24+
// Late resolution.
25+
fn f() { my_core::mem::drop(0); }
26+
//~^ ERROR failed to resolve: use of undeclared crate or module `my_core`
27+
}
28+
29+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error[E0432]: unresolved import `my_core`
2+
--> $DIR/extern-prelude-from-opaque-fail-2018.rs:21:9
3+
|
4+
LL | use my_core;
5+
| ^^^^^^^ no external crate `my_core`
6+
7+
error[E0432]: unresolved import `my_core`
8+
--> $DIR/extern-prelude-from-opaque-fail-2018.rs:8:13
9+
|
10+
LL | use my_core;
11+
| ^^^^^^^ no external crate `my_core`
12+
...
13+
LL | a!();
14+
| ---- in this macro invocation
15+
|
16+
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
18+
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
19+
--> $DIR/extern-prelude-from-opaque-fail-2018.rs:12:18
20+
|
21+
LL | fn f() { my_core::mem::drop(0); }
22+
| ^^^^^^^ use of undeclared crate or module `my_core`
23+
...
24+
LL | a!();
25+
| ---- in this macro invocation
26+
|
27+
= help: consider importing one of these items:
28+
std::mem
29+
core::mem
30+
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
32+
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
33+
--> $DIR/extern-prelude-from-opaque-fail-2018.rs:25:14
34+
|
35+
LL | fn f() { my_core::mem::drop(0); }
36+
| ^^^^^^^ use of undeclared crate or module `my_core`
37+
|
38+
help: consider importing one of these items
39+
|
40+
LL + use core::mem;
41+
|
42+
LL + use std::mem;
43+
|
44+
help: if you import `mem`, refer to it directly
45+
|
46+
LL - fn f() { my_core::mem::drop(0); }
47+
LL + fn f() { mem::drop(0); }
48+
|
49+
50+
error: aborting due to 4 previous errors
51+
52+
Some errors have detailed explanations: E0432, E0433.
53+
For more information about an error, try `rustc --explain E0432`.

tests/ui/hygiene/extern-prelude-from-opaque-fail.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// edition:2015
12
#![feature(decl_macro)]
23

34
macro a() {

tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0432]: unresolved import `my_core`
2-
--> $DIR/extern-prelude-from-opaque-fail.rs:20:9
2+
--> $DIR/extern-prelude-from-opaque-fail.rs:21:9
33
|
44
LL | use my_core;
55
| ^^^^^^^ no `my_core` in the root
66

77
error[E0432]: unresolved import `my_core`
8-
--> $DIR/extern-prelude-from-opaque-fail.rs:7:13
8+
--> $DIR/extern-prelude-from-opaque-fail.rs:8:13
99
|
1010
LL | use my_core;
1111
| ^^^^^^^ no `my_core` in the root
@@ -16,21 +16,33 @@ LL | a!();
1616
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

1818
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
19-
--> $DIR/extern-prelude-from-opaque-fail.rs:11:18
19+
--> $DIR/extern-prelude-from-opaque-fail.rs:12:18
2020
|
2121
LL | fn f() { my_core::mem::drop(0); }
2222
| ^^^^^^^ use of undeclared crate or module `my_core`
2323
...
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27+
= help: consider importing this module:
28+
my_core::mem
2729
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
2830

2931
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
30-
--> $DIR/extern-prelude-from-opaque-fail.rs:24:14
32+
--> $DIR/extern-prelude-from-opaque-fail.rs:25:14
3133
|
3234
LL | fn f() { my_core::mem::drop(0); }
3335
| ^^^^^^^ use of undeclared crate or module `my_core`
36+
|
37+
help: consider importing this module
38+
|
39+
LL + use my_core::mem;
40+
|
41+
help: if you import `mem`, refer to it directly
42+
|
43+
LL - fn f() { my_core::mem::drop(0); }
44+
LL + fn f() { mem::drop(0); }
45+
|
3446

3547
error: aborting due to 4 previous errors
3648

tests/ui/macros/builtin-prelude-no-accidents.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ error[E0433]: failed to resolve: use of undeclared crate or module `vec`
2626
LL | type B = vec::Vec<u8>;
2727
| ^^^ use of undeclared crate or module `vec`
2828
|
29-
help: a struct with a similar name exists
30-
|
31-
LL | type B = Vec::Vec<u8>;
32-
| ~~~
3329
help: consider importing this module
3430
|
3531
LL + use std::vec;

tests/ui/resolve/112590-2.fixed

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-rustfix
2+
use std::vec;
3+
4+
use std::sync::atomic::AtomicBool;
5+
6+
mod foo {
7+
pub mod bar {
8+
pub mod baz {
9+
pub use std::vec::Vec as MyVec;
10+
}
11+
}
12+
}
13+
14+
mod u {
15+
use foo::bar::baz::MyVec;
16+
17+
fn _a() {
18+
let _: Vec<i32> = MyVec::new(); //~ ERROR failed to resolve
19+
}
20+
}
21+
22+
mod v {
23+
use foo::bar::baz::MyVec;
24+
25+
fn _b() {
26+
let _: Vec<i32> = MyVec::new(); //~ ERROR failed to resolve
27+
}
28+
}
29+
30+
fn main() {
31+
let _t: Vec<i32> = Vec::new(); //~ ERROR failed to resolve
32+
type _B = vec::Vec::<u8>; //~ ERROR failed to resolve
33+
let _t = AtomicBool::new(true); //~ ERROR failed to resolve
34+
}

0 commit comments

Comments
 (0)