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 {
32
32
} ;
33
33
}
34
34
35
- diagnostics ! [ UnresolvedModule , MissingFields ] ;
35
+ diagnostics ! [ UnresolvedModule , UnresolvedExternCrate , MissingFields ] ;
36
36
37
37
#[ derive( Debug ) ]
38
38
pub struct UnresolvedModule {
39
39
pub decl : InFile < AstPtr < ast:: Module > > ,
40
40
pub candidate : String ,
41
41
}
42
42
43
- // Diagnostic: unresolved-extern-crate
44
- //
45
- // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
46
43
#[ derive( Debug ) ]
47
44
pub 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 > > ,
65
46
}
66
47
67
48
#[ derive( Debug ) ]
Original file line number Diff line number Diff line change @@ -484,10 +484,12 @@ impl Module {
484
484
}
485
485
DefDiagnosticKind :: UnresolvedExternCrate { ast } => {
486
486
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
+ ) ;
491
493
}
492
494
493
495
DefDiagnosticKind :: UnresolvedImport { id, index } => {
Original file line number Diff line number Diff line change @@ -25,35 +25,6 @@ fn unresolved_import() {
25
25
) ;
26
26
}
27
27
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
-
57
28
#[ test]
58
29
fn dedup_unresolved_import_from_unresolved_crate ( ) {
59
30
check_diagnostics (
Original file line number Diff line number Diff line change 5
5
//! original files. So we need to map the ranges.
6
6
7
7
mod unresolved_module;
8
+ mod unresolved_extern_crate;
8
9
mod missing_fields;
9
10
10
11
mod fixes;
@@ -229,8 +230,10 @@ pub(crate) fn diagnostics(
229
230
230
231
let ctx = DiagnosticsContext { config, sema, resolve } ;
231
232
for diag in diags {
233
+ #[ rustfmt:: skip]
232
234
let d = match diag {
233
235
AnyDiagnostic :: UnresolvedModule ( d) => unresolved_module:: unresolved_module ( & ctx, & d) ,
236
+ AnyDiagnostic :: UnresolvedExternCrate ( d) => unresolved_extern_crate:: unresolved_extern_crate ( & ctx, & d) ,
234
237
AnyDiagnostic :: MissingFields ( d) => missing_fields:: missing_fields ( & ctx, & d) ,
235
238
} ;
236
239
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