File tree Expand file tree Collapse file tree 5 files changed +60
-54
lines changed
hir_def/src/nameres/tests Expand file tree Collapse file tree 5 files changed +60
-54
lines changed Original file line number Diff line number Diff line change @@ -32,36 +32,17 @@ macro_rules! diagnostics {
3232 } ;
3333}
3434
35- diagnostics ! [ UnresolvedModule , MissingFields ] ;
35+ diagnostics ! [ UnresolvedModule , UnresolvedExternCrate , MissingFields ] ;
3636
3737#[ derive( Debug ) ]
3838pub struct UnresolvedModule {
3939 pub decl : InFile < AstPtr < ast:: Module > > ,
4040 pub candidate : String ,
4141}
4242
43- // Diagnostic: unresolved-extern-crate
44- //
45- // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
4643#[ derive( Debug ) ]
4744pub struct UnresolvedExternCrate {
48- pub file : HirFileId ,
49- pub item : AstPtr < ast:: ExternCrate > ,
50- }
51-
52- impl Diagnostic for UnresolvedExternCrate {
53- fn code ( & self ) -> DiagnosticCode {
54- DiagnosticCode ( "unresolved-extern-crate" )
55- }
56- fn message ( & self ) -> String {
57- "unresolved extern crate" . to_string ( )
58- }
59- fn display_source ( & self ) -> InFile < SyntaxNodePtr > {
60- InFile :: new ( self . file , self . item . clone ( ) . into ( ) )
61- }
62- fn as_any ( & self ) -> & ( dyn Any + Send + ' static ) {
63- self
64- }
45+ pub decl : InFile < AstPtr < ast:: ExternCrate > > ,
6546}
6647
6748#[ derive( Debug ) ]
Original file line number Diff line number Diff line change @@ -484,10 +484,12 @@ impl Module {
484484 }
485485 DefDiagnosticKind :: UnresolvedExternCrate { ast } => {
486486 let item = ast. to_node ( db. upcast ( ) ) ;
487- sink. push ( UnresolvedExternCrate {
488- file : ast. file_id ,
489- item : AstPtr :: new ( & item) ,
490- } ) ;
487+ acc. push (
488+ UnresolvedExternCrate {
489+ decl : InFile :: new ( ast. file_id , AstPtr :: new ( & item) ) ,
490+ }
491+ . into ( ) ,
492+ ) ;
491493 }
492494
493495 DefDiagnosticKind :: UnresolvedImport { id, index } => {
Original file line number Diff line number Diff line change @@ -25,35 +25,6 @@ fn unresolved_import() {
2525 ) ;
2626}
2727
28- #[ test]
29- fn unresolved_extern_crate ( ) {
30- check_diagnostics (
31- r"
32- //- /main.rs crate:main deps:core
33- extern crate core;
34- extern crate doesnotexist;
35- //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate
36- //- /lib.rs crate:core
37- " ,
38- ) ;
39- }
40-
41- #[ test]
42- fn extern_crate_self_as ( ) {
43- cov_mark:: check!( extern_crate_self_as) ;
44- check_diagnostics (
45- r"
46- //- /lib.rs
47- extern crate doesnotexist;
48- //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate
49- // Should not error.
50- extern crate self as foo;
51- struct Foo;
52- use foo::Foo as Bar;
53- " ,
54- ) ;
55- }
56-
5728#[ test]
5829fn dedup_unresolved_import_from_unresolved_crate ( ) {
5930 check_diagnostics (
Original file line number Diff line number Diff line change 55//! original files. So we need to map the ranges.
66
77mod unresolved_module;
8+ mod unresolved_extern_crate;
89mod missing_fields;
910
1011mod fixes;
@@ -229,8 +230,10 @@ pub(crate) fn diagnostics(
229230
230231 let ctx = DiagnosticsContext { config, sema, resolve } ;
231232 for diag in diags {
233+ #[ rustfmt:: skip]
232234 let d = match diag {
233235 AnyDiagnostic :: UnresolvedModule ( d) => unresolved_module:: unresolved_module ( & ctx, & d) ,
236+ AnyDiagnostic :: UnresolvedExternCrate ( d) => unresolved_extern_crate:: unresolved_extern_crate ( & ctx, & d) ,
234237 AnyDiagnostic :: MissingFields ( d) => missing_fields:: missing_fields ( & ctx, & d) ,
235238 } ;
236239 if let Some ( code) = d. code {
Original file line number Diff line number Diff line change 1+ use crate :: diagnostics:: { Diagnostic , DiagnosticsContext } ;
2+
3+ // Diagnostic: unresolved-extern-crate
4+ //
5+ // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
6+ pub ( super ) fn unresolved_extern_crate (
7+ ctx : & DiagnosticsContext < ' _ > ,
8+ d : & hir:: UnresolvedExternCrate ,
9+ ) -> Diagnostic {
10+ Diagnostic :: new (
11+ "unresolved-extern-crate" ,
12+ "unresolved extern crate" ,
13+ ctx. sema . diagnostics_display_range ( d. decl . clone ( ) . map ( |it| it. into ( ) ) ) . range ,
14+ )
15+ }
16+
17+ #[ cfg( test) ]
18+ mod tests {
19+ use crate :: diagnostics:: tests:: check_diagnostics;
20+
21+ #[ test]
22+ fn unresolved_extern_crate ( ) {
23+ check_diagnostics (
24+ r#"
25+ //- /main.rs crate:main deps:core
26+ extern crate core;
27+ extern crate doesnotexist;
28+ //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate
29+ //- /lib.rs crate:core
30+ "# ,
31+ ) ;
32+ }
33+
34+ #[ test]
35+ fn extern_crate_self_as ( ) {
36+ cov_mark:: check!( extern_crate_self_as) ;
37+ check_diagnostics (
38+ r#"
39+ //- /lib.rs
40+ extern crate doesnotexist;
41+ //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate
42+ // Should not error.
43+ extern crate self as foo;
44+ struct Foo;
45+ use foo::Foo as Bar;
46+ "# ,
47+ ) ;
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments