diff options
29 files changed, 1025 insertions, 176 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3fb419571..af59aa1b6 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -953,6 +953,16 @@ impl TypeParam { | |||
953 | pub fn module(self, db: &dyn HirDatabase) -> Module { | 953 | pub fn module(self, db: &dyn HirDatabase) -> Module { |
954 | self.id.parent.module(db.upcast()).into() | 954 | self.id.parent.module(db.upcast()).into() |
955 | } | 955 | } |
956 | |||
957 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
958 | let resolver = self.id.parent.resolver(db.upcast()); | ||
959 | let environment = TraitEnvironment::lower(db, &resolver); | ||
960 | let ty = Ty::Placeholder(self.id); | ||
961 | Type { | ||
962 | krate: self.id.parent.module(db.upcast()).krate, | ||
963 | ty: InEnvironment { value: ty, environment }, | ||
964 | } | ||
965 | } | ||
956 | } | 966 | } |
957 | 967 | ||
958 | // FIXME: rename from `ImplDef` to `Impl` | 968 | // FIXME: rename from `ImplDef` to `Impl` |
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 86bfb416c..a0a0f234b 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs | |||
@@ -9,6 +9,7 @@ use hir_def::{ | |||
9 | AsMacroCall, TraitId, | 9 | AsMacroCall, TraitId, |
10 | }; | 10 | }; |
11 | use hir_expand::ExpansionInfo; | 11 | use hir_expand::ExpansionInfo; |
12 | use hir_ty::associated_type_shorthand_candidates; | ||
12 | use itertools::Itertools; | 13 | use itertools::Itertools; |
13 | use ra_db::{FileId, FileRange}; | 14 | use ra_db::{FileId, FileRange}; |
14 | use ra_prof::profile; | 15 | use ra_prof::profile; |
@@ -24,8 +25,9 @@ use crate::{ | |||
24 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, | 25 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, |
25 | source_analyzer::{resolve_hir_path, SourceAnalyzer}, | 26 | source_analyzer::{resolve_hir_path, SourceAnalyzer}, |
26 | AssocItem, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef, | 27 | AssocItem, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef, |
27 | Name, Origin, Path, ScopeDef, Trait, Type, TypeParam, | 28 | Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, |
28 | }; | 29 | }; |
30 | use resolver::TypeNs; | ||
29 | 31 | ||
30 | #[derive(Debug, Clone, PartialEq, Eq)] | 32 | #[derive(Debug, Clone, PartialEq, Eq)] |
31 | pub enum PathResolution { | 33 | pub enum PathResolution { |
@@ -40,6 +42,44 @@ pub enum PathResolution { | |||
40 | AssocItem(AssocItem), | 42 | AssocItem(AssocItem), |
41 | } | 43 | } |
42 | 44 | ||
45 | impl PathResolution { | ||
46 | fn in_type_ns(&self) -> Option<TypeNs> { | ||
47 | match self { | ||
48 | PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())), | ||
49 | PathResolution::Def(ModuleDef::BuiltinType(builtin)) => { | ||
50 | Some(TypeNs::BuiltinType(*builtin)) | ||
51 | } | ||
52 | PathResolution::Def(ModuleDef::Const(_)) | ||
53 | | PathResolution::Def(ModuleDef::EnumVariant(_)) | ||
54 | | PathResolution::Def(ModuleDef::Function(_)) | ||
55 | | PathResolution::Def(ModuleDef::Module(_)) | ||
56 | | PathResolution::Def(ModuleDef::Static(_)) | ||
57 | | PathResolution::Def(ModuleDef::Trait(_)) => None, | ||
58 | PathResolution::Def(ModuleDef::TypeAlias(alias)) => { | ||
59 | Some(TypeNs::TypeAliasId((*alias).into())) | ||
60 | } | ||
61 | PathResolution::Local(_) | PathResolution::Macro(_) => None, | ||
62 | PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())), | ||
63 | PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())), | ||
64 | PathResolution::AssocItem(AssocItem::Const(_)) | ||
65 | | PathResolution::AssocItem(AssocItem::Function(_)) => None, | ||
66 | PathResolution::AssocItem(AssocItem::TypeAlias(alias)) => { | ||
67 | Some(TypeNs::TypeAliasId((*alias).into())) | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | |||
72 | /// Returns an iterator over associated types that may be specified after this path (using | ||
73 | /// `Ty::Assoc` syntax). | ||
74 | pub fn assoc_type_shorthand_candidates<R>( | ||
75 | &self, | ||
76 | db: &dyn HirDatabase, | ||
77 | mut cb: impl FnMut(TypeAlias) -> Option<R>, | ||
78 | ) -> Option<R> { | ||
79 | associated_type_shorthand_candidates(db, self.in_type_ns()?, |_, _, id| cb(id.into())) | ||
80 | } | ||
81 | } | ||
82 | |||
43 | /// Primary API to get semantic information, like types, from syntax trees. | 83 | /// Primary API to get semantic information, like types, from syntax trees. |
44 | pub struct Semantics<'db, DB> { | 84 | pub struct Semantics<'db, DB> { |
45 | pub db: &'db DB, | 85 | pub db: &'db DB, |
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index a8ef32ec5..a6f56c661 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs | |||
@@ -66,7 +66,8 @@ pub use autoderef::autoderef; | |||
66 | pub use infer::{InferTy, InferenceResult}; | 66 | pub use infer::{InferTy, InferenceResult}; |
67 | pub use lower::CallableDef; | 67 | pub use lower::CallableDef; |
68 | pub use lower::{ | 68 | pub use lower::{ |
69 | callable_item_sig, ImplTraitLoweringMode, TyDefId, TyLoweringContext, ValueTyDefId, | 69 | associated_type_shorthand_candidates, callable_item_sig, ImplTraitLoweringMode, TyDefId, |
70 | TyLoweringContext, ValueTyDefId, | ||
70 | }; | 71 | }; |
71 | pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; | 72 | pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; |
72 | 73 | ||
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index a6f893037..9ad6dbe07 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs | |||
@@ -17,9 +17,9 @@ use hir_def::{ | |||
17 | path::{GenericArg, Path, PathSegment, PathSegments}, | 17 | path::{GenericArg, Path, PathSegment, PathSegments}, |
18 | resolver::{HasResolver, Resolver, TypeNs}, | 18 | resolver::{HasResolver, Resolver, TypeNs}, |
19 | type_ref::{TypeBound, TypeRef}, | 19 | type_ref::{TypeBound, TypeRef}, |
20 | AdtId, AssocContainerId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, | 20 | AdtId, AssocContainerId, AssocItemId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, |
21 | ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, | 21 | HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, |
22 | VariantId, | 22 | UnionId, VariantId, |
23 | }; | 23 | }; |
24 | use ra_arena::map::ArenaMap; | 24 | use ra_arena::map::ArenaMap; |
25 | use ra_db::CrateId; | 25 | use ra_db::CrateId; |
@@ -34,6 +34,7 @@ use crate::{ | |||
34 | Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, PolyFnSig, ProjectionPredicate, | 34 | Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, PolyFnSig, ProjectionPredicate, |
35 | ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, | 35 | ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, |
36 | }; | 36 | }; |
37 | use hir_expand::name::Name; | ||
37 | 38 | ||
38 | #[derive(Debug)] | 39 | #[derive(Debug)] |
39 | pub struct TyLoweringContext<'a> { | 40 | pub struct TyLoweringContext<'a> { |
@@ -383,61 +384,38 @@ impl Ty { | |||
383 | res: Option<TypeNs>, | 384 | res: Option<TypeNs>, |
384 | segment: PathSegment<'_>, | 385 | segment: PathSegment<'_>, |
385 | ) -> Ty { | 386 | ) -> Ty { |
386 | let traits_from_env: Vec<_> = match res { | 387 | if let Some(res) = res { |
387 | Some(TypeNs::SelfType(impl_id)) => match ctx.db.impl_trait(impl_id) { | 388 | let ty = |
388 | None => return Ty::Unknown, | 389 | associated_type_shorthand_candidates(ctx.db, res, move |name, t, associated_ty| { |
389 | Some(trait_ref) => vec![trait_ref.value], | 390 | if name == segment.name { |
390 | }, | 391 | let substs = match ctx.type_param_mode { |
391 | Some(TypeNs::GenericParam(param_id)) => { | 392 | TypeParamLoweringMode::Placeholder => { |
392 | let predicates = ctx.db.generic_predicates_for_param(param_id); | 393 | // if we're lowering to placeholders, we have to put |
393 | let mut traits_: Vec<_> = predicates | 394 | // them in now |
394 | .iter() | 395 | let s = Substs::type_params( |
395 | .filter_map(|pred| match &pred.value { | 396 | ctx.db, |
396 | GenericPredicate::Implemented(tr) => Some(tr.clone()), | 397 | ctx.resolver.generic_def().expect( |
397 | _ => None, | 398 | "there should be generics if there's a generic param", |
398 | }) | 399 | ), |
399 | .collect(); | 400 | ); |
400 | // Handle `Self::Type` referring to own associated type in trait definitions | 401 | t.substs.clone().subst_bound_vars(&s) |
401 | if let GenericDefId::TraitId(trait_id) = param_id.parent { | 402 | } |
402 | let generics = generics(ctx.db.upcast(), trait_id.into()); | 403 | TypeParamLoweringMode::Variable => t.substs.clone(), |
403 | if generics.params.types[param_id.local_id].provenance | ||
404 | == TypeParamProvenance::TraitSelf | ||
405 | { | ||
406 | let trait_ref = TraitRef { | ||
407 | trait_: trait_id, | ||
408 | substs: Substs::bound_vars(&generics, DebruijnIndex::INNERMOST), | ||
409 | }; | 404 | }; |
410 | traits_.push(trait_ref); | 405 | // FIXME handle type parameters on the segment |
406 | return Some(Ty::Projection(ProjectionTy { | ||
407 | associated_ty, | ||
408 | parameters: substs, | ||
409 | })); | ||
411 | } | 410 | } |
412 | } | 411 | |
413 | traits_ | 412 | None |
414 | } | 413 | }); |
415 | _ => return Ty::Unknown, | 414 | |
416 | }; | 415 | ty.unwrap_or(Ty::Unknown) |
417 | let traits = traits_from_env.into_iter().flat_map(|t| all_super_trait_refs(ctx.db, t)); | 416 | } else { |
418 | for t in traits { | 417 | Ty::Unknown |
419 | if let Some(associated_ty) = | ||
420 | ctx.db.trait_data(t.trait_).associated_type_by_name(&segment.name) | ||
421 | { | ||
422 | let substs = match ctx.type_param_mode { | ||
423 | TypeParamLoweringMode::Placeholder => { | ||
424 | // if we're lowering to placeholders, we have to put | ||
425 | // them in now | ||
426 | let s = Substs::type_params( | ||
427 | ctx.db, | ||
428 | ctx.resolver | ||
429 | .generic_def() | ||
430 | .expect("there should be generics if there's a generic param"), | ||
431 | ); | ||
432 | t.substs.subst_bound_vars(&s) | ||
433 | } | ||
434 | TypeParamLoweringMode::Variable => t.substs, | ||
435 | }; | ||
436 | // FIXME handle (forbid) type parameters on the segment | ||
437 | return Ty::Projection(ProjectionTy { associated_ty, parameters: substs }); | ||
438 | } | ||
439 | } | 418 | } |
440 | Ty::Unknown | ||
441 | } | 419 | } |
442 | 420 | ||
443 | fn from_hir_path_inner( | 421 | fn from_hir_path_inner( |
@@ -694,6 +672,61 @@ pub fn callable_item_sig(db: &dyn HirDatabase, def: CallableDef) -> PolyFnSig { | |||
694 | } | 672 | } |
695 | } | 673 | } |
696 | 674 | ||
675 | pub fn associated_type_shorthand_candidates<R>( | ||
676 | db: &dyn HirDatabase, | ||
677 | res: TypeNs, | ||
678 | mut cb: impl FnMut(&Name, &TraitRef, TypeAliasId) -> Option<R>, | ||
679 | ) -> Option<R> { | ||
680 | let traits_from_env: Vec<_> = match res { | ||
681 | TypeNs::SelfType(impl_id) => match db.impl_trait(impl_id) { | ||
682 | None => vec![], | ||
683 | Some(trait_ref) => vec![trait_ref.value], | ||
684 | }, | ||
685 | TypeNs::GenericParam(param_id) => { | ||
686 | let predicates = db.generic_predicates_for_param(param_id); | ||
687 | let mut traits_: Vec<_> = predicates | ||
688 | .iter() | ||
689 | .filter_map(|pred| match &pred.value { | ||
690 | GenericPredicate::Implemented(tr) => Some(tr.clone()), | ||
691 | _ => None, | ||
692 | }) | ||
693 | .collect(); | ||
694 | // Handle `Self::Type` referring to own associated type in trait definitions | ||
695 | if let GenericDefId::TraitId(trait_id) = param_id.parent { | ||
696 | let generics = generics(db.upcast(), trait_id.into()); | ||
697 | if generics.params.types[param_id.local_id].provenance | ||
698 | == TypeParamProvenance::TraitSelf | ||
699 | { | ||
700 | let trait_ref = TraitRef { | ||
701 | trait_: trait_id, | ||
702 | substs: Substs::bound_vars(&generics, DebruijnIndex::INNERMOST), | ||
703 | }; | ||
704 | traits_.push(trait_ref); | ||
705 | } | ||
706 | } | ||
707 | traits_ | ||
708 | } | ||
709 | _ => vec![], | ||
710 | }; | ||
711 | |||
712 | for t in traits_from_env.into_iter().flat_map(move |t| all_super_trait_refs(db, t)) { | ||
713 | let data = db.trait_data(t.trait_); | ||
714 | |||
715 | for (name, assoc_id) in &data.items { | ||
716 | match assoc_id { | ||
717 | AssocItemId::TypeAliasId(alias) => { | ||
718 | if let Some(result) = cb(name, &t, *alias) { | ||
719 | return Some(result); | ||
720 | } | ||
721 | } | ||
722 | AssocItemId::FunctionId(_) | AssocItemId::ConstId(_) => {} | ||
723 | } | ||
724 | } | ||
725 | } | ||
726 | |||
727 | None | ||
728 | } | ||
729 | |||
697 | /// Build the type of all specific fields of a struct or enum variant. | 730 | /// Build the type of all specific fields of a struct or enum variant. |
698 | pub(crate) fn field_types_query( | 731 | pub(crate) fn field_types_query( |
699 | db: &dyn HirDatabase, | 732 | db: &dyn HirDatabase, |
diff --git a/crates/ra_ide/src/completion/complete_qualified_path.rs b/crates/ra_ide/src/completion/complete_qualified_path.rs index dd10f74e6..aa56a5cd8 100644 --- a/crates/ra_ide/src/completion/complete_qualified_path.rs +++ b/crates/ra_ide/src/completion/complete_qualified_path.rs | |||
@@ -5,19 +5,29 @@ use ra_syntax::AstNode; | |||
5 | use test_utils::tested_by; | 5 | use test_utils::tested_by; |
6 | 6 | ||
7 | use crate::completion::{CompletionContext, Completions}; | 7 | use crate::completion::{CompletionContext, Completions}; |
8 | use rustc_hash::FxHashSet; | ||
8 | 9 | ||
9 | pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { | 10 | pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { |
10 | let path = match &ctx.path_prefix { | 11 | let path = match &ctx.path_prefix { |
11 | Some(path) => path.clone(), | 12 | Some(path) => path.clone(), |
12 | _ => return, | 13 | _ => return, |
13 | }; | 14 | }; |
14 | let def = match ctx.scope().resolve_hir_path(&path) { | 15 | let scope = ctx.scope(); |
15 | Some(PathResolution::Def(def)) => def, | 16 | let context_module = scope.module(); |
16 | _ => return, | 17 | |
18 | let res = match scope.resolve_hir_path(&path) { | ||
19 | Some(res) => res, | ||
20 | None => return, | ||
17 | }; | 21 | }; |
18 | let context_module = ctx.scope().module(); | 22 | |
19 | match def { | 23 | // Add associated types on type parameters and `Self`. |
20 | hir::ModuleDef::Module(module) => { | 24 | res.assoc_type_shorthand_candidates(ctx.db, |alias| { |
25 | acc.add_type_alias(ctx, alias); | ||
26 | None::<()> | ||
27 | }); | ||
28 | |||
29 | match res { | ||
30 | PathResolution::Def(hir::ModuleDef::Module(module)) => { | ||
21 | let module_scope = module.scope(ctx.db, context_module); | 31 | let module_scope = module.scope(ctx.db, context_module); |
22 | for (name, def) in module_scope { | 32 | for (name, def) in module_scope { |
23 | if ctx.use_item_syntax.is_some() { | 33 | if ctx.use_item_syntax.is_some() { |
@@ -35,7 +45,8 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |||
35 | acc.add_resolution(ctx, name.to_string(), &def); | 45 | acc.add_resolution(ctx, name.to_string(), &def); |
36 | } | 46 | } |
37 | } | 47 | } |
38 | hir::ModuleDef::Adt(_) | hir::ModuleDef::TypeAlias(_) => { | 48 | PathResolution::Def(def @ hir::ModuleDef::Adt(_)) |
49 | | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) => { | ||
39 | if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { | 50 | if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { |
40 | for variant in e.variants(ctx.db) { | 51 | for variant in e.variants(ctx.db) { |
41 | acc.add_enum_variant(ctx, variant, None); | 52 | acc.add_enum_variant(ctx, variant, None); |
@@ -46,8 +57,10 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |||
46 | hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), | 57 | hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), |
47 | _ => unreachable!(), | 58 | _ => unreachable!(), |
48 | }; | 59 | }; |
49 | // Iterate assoc types separately | 60 | |
50 | // FIXME: complete T::AssocType | 61 | // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType. |
62 | // (where AssocType is defined on a trait, not an inherent impl) | ||
63 | |||
51 | let krate = ctx.krate; | 64 | let krate = ctx.krate; |
52 | if let Some(krate) = krate { | 65 | if let Some(krate) = krate { |
53 | let traits_in_scope = ctx.scope().traits_in_scope(); | 66 | let traits_in_scope = ctx.scope().traits_in_scope(); |
@@ -65,6 +78,7 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |||
65 | None::<()> | 78 | None::<()> |
66 | }); | 79 | }); |
67 | 80 | ||
81 | // Iterate assoc types separately | ||
68 | ty.iterate_impl_items(ctx.db, krate, |item| { | 82 | ty.iterate_impl_items(ctx.db, krate, |item| { |
69 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | 83 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { |
70 | return None; | 84 | return None; |
@@ -77,7 +91,8 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |||
77 | }); | 91 | }); |
78 | } | 92 | } |
79 | } | 93 | } |
80 | hir::ModuleDef::Trait(t) => { | 94 | PathResolution::Def(hir::ModuleDef::Trait(t)) => { |
95 | // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`. | ||
81 | for item in t.items(ctx.db) { | 96 | for item in t.items(ctx.db) { |
82 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | 97 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { |
83 | continue; | 98 | continue; |
@@ -91,8 +106,38 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |||
91 | } | 106 | } |
92 | } | 107 | } |
93 | } | 108 | } |
109 | PathResolution::TypeParam(_) | PathResolution::SelfType(_) => { | ||
110 | if let Some(krate) = ctx.krate { | ||
111 | let ty = match res { | ||
112 | PathResolution::TypeParam(param) => param.ty(ctx.db), | ||
113 | PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db), | ||
114 | _ => return, | ||
115 | }; | ||
116 | |||
117 | let traits_in_scope = ctx.scope().traits_in_scope(); | ||
118 | let mut seen = FxHashSet::default(); | ||
119 | ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| { | ||
120 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
121 | return None; | ||
122 | } | ||
123 | |||
124 | // We might iterate candidates of a trait multiple times here, so deduplicate | ||
125 | // them. | ||
126 | if seen.insert(item) { | ||
127 | match item { | ||
128 | hir::AssocItem::Function(func) => { | ||
129 | acc.add_function(ctx, func, None); | ||
130 | } | ||
131 | hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), | ||
132 | hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), | ||
133 | } | ||
134 | } | ||
135 | None::<()> | ||
136 | }); | ||
137 | } | ||
138 | } | ||
94 | _ => {} | 139 | _ => {} |
95 | }; | 140 | } |
96 | } | 141 | } |
97 | 142 | ||
98 | #[cfg(test)] | 143 | #[cfg(test)] |
@@ -844,6 +889,211 @@ mod tests { | |||
844 | } | 889 | } |
845 | 890 | ||
846 | #[test] | 891 | #[test] |
892 | fn completes_ty_param_assoc_ty() { | ||
893 | assert_debug_snapshot!( | ||
894 | do_reference_completion( | ||
895 | " | ||
896 | //- /lib.rs | ||
897 | trait Super { | ||
898 | type Ty; | ||
899 | const CONST: u8; | ||
900 | fn func() {} | ||
901 | fn method(&self) {} | ||
902 | } | ||
903 | |||
904 | trait Sub: Super { | ||
905 | type SubTy; | ||
906 | const C2: (); | ||
907 | fn subfunc() {} | ||
908 | fn submethod(&self) {} | ||
909 | } | ||
910 | |||
911 | fn foo<T: Sub>() { | ||
912 | T::<|> | ||
913 | } | ||
914 | " | ||
915 | ), | ||
916 | @r###" | ||
917 | [ | ||
918 | CompletionItem { | ||
919 | label: "C2", | ||
920 | source_range: 219..219, | ||
921 | delete: 219..219, | ||
922 | insert: "C2", | ||
923 | kind: Const, | ||
924 | detail: "const C2: ();", | ||
925 | }, | ||
926 | CompletionItem { | ||
927 | label: "CONST", | ||
928 | source_range: 219..219, | ||
929 | delete: 219..219, | ||
930 | insert: "CONST", | ||
931 | kind: Const, | ||
932 | detail: "const CONST: u8;", | ||
933 | }, | ||
934 | CompletionItem { | ||
935 | label: "SubTy", | ||
936 | source_range: 219..219, | ||
937 | delete: 219..219, | ||
938 | insert: "SubTy", | ||
939 | kind: TypeAlias, | ||
940 | detail: "type SubTy;", | ||
941 | }, | ||
942 | CompletionItem { | ||
943 | label: "Ty", | ||
944 | source_range: 219..219, | ||
945 | delete: 219..219, | ||
946 | insert: "Ty", | ||
947 | kind: TypeAlias, | ||
948 | detail: "type Ty;", | ||
949 | }, | ||
950 | CompletionItem { | ||
951 | label: "func()", | ||
952 | source_range: 219..219, | ||
953 | delete: 219..219, | ||
954 | insert: "func()$0", | ||
955 | kind: Function, | ||
956 | lookup: "func", | ||
957 | detail: "fn func()", | ||
958 | }, | ||
959 | CompletionItem { | ||
960 | label: "method()", | ||
961 | source_range: 219..219, | ||
962 | delete: 219..219, | ||
963 | insert: "method()$0", | ||
964 | kind: Method, | ||
965 | lookup: "method", | ||
966 | detail: "fn method(&self)", | ||
967 | }, | ||
968 | CompletionItem { | ||
969 | label: "subfunc()", | ||
970 | source_range: 219..219, | ||
971 | delete: 219..219, | ||
972 | insert: "subfunc()$0", | ||
973 | kind: Function, | ||
974 | lookup: "subfunc", | ||
975 | detail: "fn subfunc()", | ||
976 | }, | ||
977 | CompletionItem { | ||
978 | label: "submethod()", | ||
979 | source_range: 219..219, | ||
980 | delete: 219..219, | ||
981 | insert: "submethod()$0", | ||
982 | kind: Method, | ||
983 | lookup: "submethod", | ||
984 | detail: "fn submethod(&self)", | ||
985 | }, | ||
986 | ] | ||
987 | "### | ||
988 | ); | ||
989 | } | ||
990 | |||
991 | #[test] | ||
992 | fn completes_self_param_assoc_ty() { | ||
993 | assert_debug_snapshot!( | ||
994 | do_reference_completion( | ||
995 | " | ||
996 | //- /lib.rs | ||
997 | trait Super { | ||
998 | type Ty; | ||
999 | const CONST: u8 = 0; | ||
1000 | fn func() {} | ||
1001 | fn method(&self) {} | ||
1002 | } | ||
1003 | |||
1004 | trait Sub: Super { | ||
1005 | type SubTy; | ||
1006 | const C2: () = (); | ||
1007 | fn subfunc() {} | ||
1008 | fn submethod(&self) {} | ||
1009 | } | ||
1010 | |||
1011 | struct Wrap<T>(T); | ||
1012 | impl<T> Super for Wrap<T> {} | ||
1013 | impl<T> Sub for Wrap<T> { | ||
1014 | fn subfunc() { | ||
1015 | // Should be able to assume `Self: Sub + Super` | ||
1016 | Self::<|> | ||
1017 | } | ||
1018 | } | ||
1019 | " | ||
1020 | ), | ||
1021 | @r###" | ||
1022 | [ | ||
1023 | CompletionItem { | ||
1024 | label: "C2", | ||
1025 | source_range: 365..365, | ||
1026 | delete: 365..365, | ||
1027 | insert: "C2", | ||
1028 | kind: Const, | ||
1029 | detail: "const C2: () = ();", | ||
1030 | }, | ||
1031 | CompletionItem { | ||
1032 | label: "CONST", | ||
1033 | source_range: 365..365, | ||
1034 | delete: 365..365, | ||
1035 | insert: "CONST", | ||
1036 | kind: Const, | ||
1037 | detail: "const CONST: u8 = 0;", | ||
1038 | }, | ||
1039 | CompletionItem { | ||
1040 | label: "SubTy", | ||
1041 | source_range: 365..365, | ||
1042 | delete: 365..365, | ||
1043 | insert: "SubTy", | ||
1044 | kind: TypeAlias, | ||
1045 | detail: "type SubTy;", | ||
1046 | }, | ||
1047 | CompletionItem { | ||
1048 | label: "Ty", | ||
1049 | source_range: 365..365, | ||
1050 | delete: 365..365, | ||
1051 | insert: "Ty", | ||
1052 | kind: TypeAlias, | ||
1053 | detail: "type Ty;", | ||
1054 | }, | ||
1055 | CompletionItem { | ||
1056 | label: "func()", | ||
1057 | source_range: 365..365, | ||
1058 | delete: 365..365, | ||
1059 | insert: "func()$0", | ||
1060 | kind: Function, | ||
1061 | lookup: "func", | ||
1062 | detail: "fn func()", | ||
1063 | }, | ||
1064 | CompletionItem { | ||
1065 | label: "method()", | ||
1066 | source_range: 365..365, | ||
1067 | delete: 365..365, | ||
1068 | insert: "method()$0", | ||
1069 | kind: Method, | ||
1070 | lookup: "method", | ||
1071 | detail: "fn method(&self)", | ||
1072 | }, | ||
1073 | CompletionItem { | ||
1074 | label: "subfunc()", | ||
1075 | source_range: 365..365, | ||
1076 | delete: 365..365, | ||
1077 | insert: "subfunc()$0", | ||
1078 | kind: Function, | ||
1079 | lookup: "subfunc", | ||
1080 | detail: "fn subfunc()", | ||
1081 | }, | ||
1082 | CompletionItem { | ||
1083 | label: "submethod()", | ||
1084 | source_range: 365..365, | ||
1085 | delete: 365..365, | ||
1086 | insert: "submethod()$0", | ||
1087 | kind: Method, | ||
1088 | lookup: "submethod", | ||
1089 | detail: "fn submethod(&self)", | ||
1090 | }, | ||
1091 | ] | ||
1092 | "### | ||
1093 | ); | ||
1094 | } | ||
1095 | |||
1096 | #[test] | ||
847 | fn completes_type_alias() { | 1097 | fn completes_type_alias() { |
848 | assert_debug_snapshot!( | 1098 | assert_debug_snapshot!( |
849 | do_reference_completion( | 1099 | do_reference_completion( |
diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index b5e2785fe..db3907fe6 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs | |||
@@ -26,6 +26,8 @@ pub struct FunctionSignature { | |||
26 | pub kind: CallableKind, | 26 | pub kind: CallableKind, |
27 | /// Optional visibility | 27 | /// Optional visibility |
28 | pub visibility: Option<String>, | 28 | pub visibility: Option<String>, |
29 | /// Qualifiers like `async`, `unsafe`, ... | ||
30 | pub qualifier: FunctionQualifier, | ||
29 | /// Name of the function | 31 | /// Name of the function |
30 | pub name: Option<String>, | 32 | pub name: Option<String>, |
31 | /// Documentation for the function | 33 | /// Documentation for the function |
@@ -46,6 +48,16 @@ pub struct FunctionSignature { | |||
46 | pub has_self_param: bool, | 48 | pub has_self_param: bool, |
47 | } | 49 | } |
48 | 50 | ||
51 | #[derive(Debug, Default)] | ||
52 | pub struct FunctionQualifier { | ||
53 | // `async` and `const` are mutually exclusive. Do we need to enforcing it here? | ||
54 | pub is_async: bool, | ||
55 | pub is_const: bool, | ||
56 | pub is_unsafe: bool, | ||
57 | /// The string `extern ".."` | ||
58 | pub extern_abi: Option<String>, | ||
59 | } | ||
60 | |||
49 | impl FunctionSignature { | 61 | impl FunctionSignature { |
50 | pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self { | 62 | pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self { |
51 | self.doc = doc; | 63 | self.doc = doc; |
@@ -83,6 +95,8 @@ impl FunctionSignature { | |||
83 | FunctionSignature { | 95 | FunctionSignature { |
84 | kind: CallableKind::StructConstructor, | 96 | kind: CallableKind::StructConstructor, |
85 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), | 97 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), |
98 | // Do we need `const`? | ||
99 | qualifier: Default::default(), | ||
86 | name: node.name().map(|n| n.text().to_string()), | 100 | name: node.name().map(|n| n.text().to_string()), |
87 | ret_type: node.name().map(|n| n.text().to_string()), | 101 | ret_type: node.name().map(|n| n.text().to_string()), |
88 | parameters: params, | 102 | parameters: params, |
@@ -128,6 +142,8 @@ impl FunctionSignature { | |||
128 | FunctionSignature { | 142 | FunctionSignature { |
129 | kind: CallableKind::VariantConstructor, | 143 | kind: CallableKind::VariantConstructor, |
130 | visibility: None, | 144 | visibility: None, |
145 | // Do we need `const`? | ||
146 | qualifier: Default::default(), | ||
131 | name: Some(name), | 147 | name: Some(name), |
132 | ret_type: None, | 148 | ret_type: None, |
133 | parameters: params, | 149 | parameters: params, |
@@ -151,6 +167,7 @@ impl FunctionSignature { | |||
151 | FunctionSignature { | 167 | FunctionSignature { |
152 | kind: CallableKind::Macro, | 168 | kind: CallableKind::Macro, |
153 | visibility: None, | 169 | visibility: None, |
170 | qualifier: Default::default(), | ||
154 | name: node.name().map(|n| n.text().to_string()), | 171 | name: node.name().map(|n| n.text().to_string()), |
155 | ret_type: None, | 172 | ret_type: None, |
156 | parameters: params, | 173 | parameters: params, |
@@ -223,6 +240,12 @@ impl From<&'_ ast::FnDef> for FunctionSignature { | |||
223 | FunctionSignature { | 240 | FunctionSignature { |
224 | kind: CallableKind::Function, | 241 | kind: CallableKind::Function, |
225 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), | 242 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), |
243 | qualifier: FunctionQualifier { | ||
244 | is_async: node.async_token().is_some(), | ||
245 | is_const: node.const_token().is_some(), | ||
246 | is_unsafe: node.unsafe_token().is_some(), | ||
247 | extern_abi: node.abi().map(|n| n.to_string()), | ||
248 | }, | ||
226 | name: node.name().map(|n| n.text().to_string()), | 249 | name: node.name().map(|n| n.text().to_string()), |
227 | ret_type: node | 250 | ret_type: node |
228 | .ret_type() | 251 | .ret_type() |
@@ -246,6 +269,23 @@ impl Display for FunctionSignature { | |||
246 | write!(f, "{} ", t)?; | 269 | write!(f, "{} ", t)?; |
247 | } | 270 | } |
248 | 271 | ||
272 | if self.qualifier.is_async { | ||
273 | write!(f, "async ")?; | ||
274 | } | ||
275 | |||
276 | if self.qualifier.is_const { | ||
277 | write!(f, "const ")?; | ||
278 | } | ||
279 | |||
280 | if self.qualifier.is_unsafe { | ||
281 | write!(f, "unsafe ")?; | ||
282 | } | ||
283 | |||
284 | if let Some(extern_abi) = &self.qualifier.extern_abi { | ||
285 | // Keyword `extern` is included in the string. | ||
286 | write!(f, "{} ", extern_abi)?; | ||
287 | } | ||
288 | |||
249 | if let Some(name) = &self.name { | 289 | if let Some(name) = &self.name { |
250 | match self.kind { | 290 | match self.kind { |
251 | CallableKind::Function => write!(f, "fn {}", name)?, | 291 | CallableKind::Function => write!(f, "fn {}", name)?, |
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 58c799eca..a62f598f0 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs | |||
@@ -844,4 +844,29 @@ fn func(foo: i32) { if true { <|>foo; }; } | |||
844 | &["fn foo()\n```\n\n<- `\u{3000}` here"], | 844 | &["fn foo()\n```\n\n<- `\u{3000}` here"], |
845 | ); | 845 | ); |
846 | } | 846 | } |
847 | |||
848 | #[test] | ||
849 | fn test_hover_function_show_qualifiers() { | ||
850 | check_hover_result( | ||
851 | " | ||
852 | //- /lib.rs | ||
853 | async fn foo<|>() {} | ||
854 | ", | ||
855 | &["async fn foo()"], | ||
856 | ); | ||
857 | check_hover_result( | ||
858 | " | ||
859 | //- /lib.rs | ||
860 | pub const unsafe fn foo<|>() {} | ||
861 | ", | ||
862 | &["pub const unsafe fn foo()"], | ||
863 | ); | ||
864 | check_hover_result( | ||
865 | r#" | ||
866 | //- /lib.rs | ||
867 | pub(crate) async unsafe extern "C" fn foo<|>() {} | ||
868 | "#, | ||
869 | &[r#"pub(crate) async unsafe extern "C" fn foo()"#], | ||
870 | ); | ||
871 | } | ||
847 | } | 872 | } |
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 0d277a586..166dfc472 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs | |||
@@ -535,6 +535,22 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { | |||
535 | fn try_block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { | 535 | fn try_block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { |
536 | assert!(p.at(T![try])); | 536 | assert!(p.at(T![try])); |
537 | let m = m.unwrap_or_else(|| p.start()); | 537 | let m = m.unwrap_or_else(|| p.start()); |
538 | // Special-case `try!` as macro. | ||
539 | // This is a hack until we do proper edition support | ||
540 | if p.nth_at(1, T![!]) { | ||
541 | // test try_macro_fallback | ||
542 | // fn foo() { try!(Ok(())); } | ||
543 | let path = p.start(); | ||
544 | let path_segment = p.start(); | ||
545 | let name_ref = p.start(); | ||
546 | p.bump_remap(IDENT); | ||
547 | name_ref.complete(p, NAME_REF); | ||
548 | path_segment.complete(p, PATH_SEGMENT); | ||
549 | path.complete(p, PATH); | ||
550 | let _block_like = items::macro_call_after_excl(p); | ||
551 | return m.complete(p, MACRO_CALL); | ||
552 | } | ||
553 | |||
538 | p.bump(T![try]); | 554 | p.bump(T![try]); |
539 | block(p); | 555 | block(p); |
540 | m.complete(p, TRY_EXPR) | 556 | m.complete(p, TRY_EXPR) |
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index 433ed6812..1503a8730 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs | |||
@@ -415,6 +415,17 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { | |||
415 | if p.at(IDENT) { | 415 | if p.at(IDENT) { |
416 | name(p); | 416 | name(p); |
417 | } | 417 | } |
418 | // Special-case `macro_rules! try`. | ||
419 | // This is a hack until we do proper edition support | ||
420 | |||
421 | // test try_macro_rules | ||
422 | // macro_rules! try { () => {} } | ||
423 | if p.at(T![try]) { | ||
424 | let m = p.start(); | ||
425 | p.bump_remap(IDENT); | ||
426 | m.complete(p, NAME); | ||
427 | } | ||
428 | |||
418 | match p.current() { | 429 | match p.current() { |
419 | T!['{'] => { | 430 | T!['{'] => { |
420 | token_tree(p); | 431 | token_tree(p); |
diff --git a/crates/ra_parser/src/grammar/items/use_item.rs b/crates/ra_parser/src/grammar/items/use_item.rs index e3b991c8c..3a0c7a31a 100644 --- a/crates/ra_parser/src/grammar/items/use_item.rs +++ b/crates/ra_parser/src/grammar/items/use_item.rs | |||
@@ -47,7 +47,7 @@ fn use_tree(p: &mut Parser, top_level: bool) { | |||
47 | // use {crate::path::from::root, or::path::from::crate_name}; // Rust 2018 (with a crate named `or`) | 47 | // use {crate::path::from::root, or::path::from::crate_name}; // Rust 2018 (with a crate named `or`) |
48 | // use {path::from::root}; // Rust 2015 | 48 | // use {path::from::root}; // Rust 2015 |
49 | // use ::{some::arbritrary::path}; // Rust 2015 | 49 | // use ::{some::arbritrary::path}; // Rust 2015 |
50 | // use ::{{{crate::export}}}; // Nonsensical but perfectly legal nestnig | 50 | // use ::{{{root::export}}}; // Nonsensical but perfectly legal nesting |
51 | T!['{'] => { | 51 | T!['{'] => { |
52 | use_tree_list(p); | 52 | use_tree_list(p); |
53 | } | 53 | } |
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 2cb3ad011..3b5e05af9 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs | |||
@@ -1249,6 +1249,7 @@ pub struct PathSegment { | |||
1249 | } | 1249 | } |
1250 | impl PathSegment { | 1250 | impl PathSegment { |
1251 | pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } | 1251 | pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } |
1252 | pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) } | ||
1252 | pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } | 1253 | pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } |
1253 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | 1254 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } |
1254 | pub fn type_arg_list(&self) -> Option<TypeArgList> { support::child(&self.syntax) } | 1255 | pub fn type_arg_list(&self) -> Option<TypeArgList> { support::child(&self.syntax) } |
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 5e93895ec..f0b3dec63 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs | |||
@@ -96,6 +96,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> { | |||
96 | ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors), | 96 | ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors), |
97 | ast::Visibility(it) => validate_visibility(it, &mut errors), | 97 | ast::Visibility(it) => validate_visibility(it, &mut errors), |
98 | ast::RangeExpr(it) => validate_range_expr(it, &mut errors), | 98 | ast::RangeExpr(it) => validate_range_expr(it, &mut errors), |
99 | ast::PathSegment(it) => validate_crate_keyword_in_path_segment(it, &mut errors), | ||
99 | _ => (), | 100 | _ => (), |
100 | } | 101 | } |
101 | } | 102 | } |
@@ -222,3 +223,60 @@ fn validate_range_expr(expr: ast::RangeExpr, errors: &mut Vec<SyntaxError>) { | |||
222 | )); | 223 | )); |
223 | } | 224 | } |
224 | } | 225 | } |
226 | |||
227 | fn validate_crate_keyword_in_path_segment( | ||
228 | segment: ast::PathSegment, | ||
229 | errors: &mut Vec<SyntaxError>, | ||
230 | ) { | ||
231 | const ERR_MSG: &str = "The `crate` keyword is only allowed as the first segment of a path"; | ||
232 | |||
233 | let crate_token = match segment.crate_token() { | ||
234 | None => return, | ||
235 | Some(it) => it, | ||
236 | }; | ||
237 | |||
238 | // Disallow both ::crate and foo::crate | ||
239 | let mut path = segment.parent_path(); | ||
240 | if segment.coloncolon_token().is_some() || path.qualifier().is_some() { | ||
241 | errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); | ||
242 | return; | ||
243 | } | ||
244 | |||
245 | // For expressions and types, validation is complete, but we still have | ||
246 | // to handle invalid UseItems like this: | ||
247 | // | ||
248 | // use foo:{crate::bar::baz}; | ||
249 | // | ||
250 | // To handle this we must inspect the parent `UseItem`s and `UseTree`s | ||
251 | // but right now we're looking deep inside the nested `Path` nodes because | ||
252 | // `Path`s are left-associative: | ||
253 | // | ||
254 | // ((crate)::bar)::baz) | ||
255 | // ^ current value of path | ||
256 | // | ||
257 | // So we need to climb to the top | ||
258 | while let Some(parent) = path.parent_path() { | ||
259 | path = parent; | ||
260 | } | ||
261 | |||
262 | // Now that we've found the whole path we need to see if there's a prefix | ||
263 | // somewhere in the UseTree hierarchy. This check is arbitrarily deep | ||
264 | // because rust allows arbitrary nesting like so: | ||
265 | // | ||
266 | // use {foo::{{{{crate::bar::baz}}}}}; | ||
267 | for node in path.syntax().ancestors().skip(1) { | ||
268 | match_ast! { | ||
269 | match node { | ||
270 | ast::UseTree(it) => if let Some(tree_path) = it.path() { | ||
271 | // Even a top-level path exists within a `UseTree` so we must explicitly | ||
272 | // allow our path but disallow anything else | ||
273 | if tree_path != path { | ||
274 | errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); | ||
275 | } | ||
276 | }, | ||
277 | ast::UseTreeList(_it) => continue, | ||
278 | _ => return, | ||
279 | } | ||
280 | }; | ||
281 | } | ||
282 | } | ||
diff --git a/crates/ra_syntax/test_data/parser/err/0040_illegal_crate_kw_location.rast b/crates/ra_syntax/test_data/parser/err/0040_illegal_crate_kw_location.rast new file mode 100644 index 000000000..d2a549273 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/err/0040_illegal_crate_kw_location.rast | |||
@@ -0,0 +1,91 @@ | |||
1 | [email protected] | ||
2 | [email protected] | ||
3 | [email protected] "use" | ||
4 | [email protected] " " | ||
5 | [email protected] | ||
6 | [email protected] | ||
7 | [email protected] | ||
8 | [email protected] "::" | ||
9 | [email protected] "crate" | ||
10 | [email protected] ";" | ||
11 | [email protected] "\n" | ||
12 | [email protected] | ||
13 | [email protected] "use" | ||
14 | [email protected] " " | ||
15 | [email protected] | ||
16 | [email protected] | ||
17 | [email protected] "{" | ||
18 | [email protected] | ||
19 | [email protected] | ||
20 | [email protected] | ||
21 | [email protected] "crate" | ||
22 | [email protected] "," | ||
23 | [email protected] " " | ||
24 | [email protected] | ||
25 | [email protected] | ||
26 | [email protected] | ||
27 | [email protected] | ||
28 | [email protected] "foo" | ||
29 | [email protected] "::" | ||
30 | [email protected] | ||
31 | [email protected] "{" | ||
32 | [email protected] | ||
33 | [email protected] | ||
34 | [email protected] | ||
35 | [email protected] | ||
36 | [email protected] | ||
37 | [email protected] | ||
38 | [email protected] "crate" | ||
39 | [email protected] "::" | ||
40 | [email protected] | ||
41 | [email protected] | ||
42 | [email protected] "foo" | ||
43 | [email protected] "::" | ||
44 | [email protected] | ||
45 | [email protected] | ||
46 | [email protected] "bar" | ||
47 | [email protected] "::" | ||
48 | [email protected] | ||
49 | [email protected] | ||
50 | [email protected] "baz" | ||
51 | [email protected] "}" | ||
52 | [email protected] "}" | ||
53 | [email protected] ";" | ||
54 | [email protected] "\n" | ||
55 | [email protected] | ||
56 | [email protected] "use" | ||
57 | [email protected] " " | ||
58 | [email protected] | ||
59 | [email protected] | ||
60 | [email protected] | ||
61 | [email protected] | ||
62 | [email protected] | ||
63 | [email protected] "hello" | ||
64 | [email protected] "::" | ||
65 | [email protected] | ||
66 | [email protected] "crate" | ||
67 | [email protected] ";" | ||
68 | [email protected] "\n" | ||
69 | [email protected] | ||
70 | [email protected] "use" | ||
71 | [email protected] " " | ||
72 | [email protected] | ||
73 | [email protected] | ||
74 | [email protected] | ||
75 | [email protected] | ||
76 | [email protected] | ||
77 | [email protected] | ||
78 | [email protected] "hello" | ||
79 | [email protected] "::" | ||
80 | [email protected] | ||
81 | [email protected] "crate" | ||
82 | [email protected] "::" | ||
83 | [email protected] | ||
84 | [email protected] | ||
85 | [email protected] "there" | ||
86 | [email protected] ";" | ||
87 | [email protected] "\n" | ||
88 | error 6..11: The `crate` keyword is only allowed as the first segment of a path | ||
89 | error 31..36: The `crate` keyword is only allowed as the first segment of a path | ||
90 | error 66..71: The `crate` keyword is only allowed as the first segment of a path | ||
91 | error 84..89: The `crate` keyword is only allowed as the first segment of a path | ||
diff --git a/crates/ra_syntax/test_data/parser/err/0040_illegal_crate_kw_location.rs b/crates/ra_syntax/test_data/parser/err/0040_illegal_crate_kw_location.rs new file mode 100644 index 000000000..508def2c7 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/err/0040_illegal_crate_kw_location.rs | |||
@@ -0,0 +1,4 @@ | |||
1 | use ::crate; | ||
2 | use {crate, foo::{crate::foo::bar::baz}}; | ||
3 | use hello::crate; | ||
4 | use hello::crate::there; | ||
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rast b/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rast index bd74b44a6..cf3a90400 100644 --- a/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rast +++ b/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rast | |||
@@ -1,4 +1,4 @@ | |||
1 | [email protected]50 | 1 | [email protected]49 |
2 | [email protected] | 2 | [email protected] |
3 | [email protected] "use" | 3 | [email protected] "use" |
4 | [email protected] " " | 4 | [email protected] " " |
@@ -104,32 +104,33 @@ [email protected] | |||
104 | [email protected] " " | 104 | [email protected] " " |
105 | [email protected] "// Rust 2015" | 105 | [email protected] "// Rust 2015" |
106 | [email protected] "\n" | 106 | [email protected] "\n" |
107 | [email protected]6 | 107 | [email protected]5 |
108 | [email protected] "use" | 108 | [email protected] "use" |
109 | [email protected] " " | 109 | [email protected] " " |
110 | [email protected]5 | 110 | [email protected]4 |
111 | [email protected] "::" | 111 | [email protected] "::" |
112 | [email protected]5 | 112 | [email protected]4 |
113 | [email protected] "{" | 113 | [email protected] "{" |
114 | [email protected]4 | 114 | [email protected]3 |
115 | [email protected]4 | 115 | [email protected]3 |
116 | [email protected] "{" | 116 | [email protected] "{" |
117 | [email protected]3 | 117 | [email protected]2 |
118 | [email protected]3 | 118 | [email protected]2 |
119 | [email protected] "{" | 119 | [email protected] "{" |
120 | [email protected] | 120 | [email protected] |
121 | [email protected] | 121 | [email protected] |
122 | [email protected] | 122 | [email protected] |
123 | [email protected] | 123 | [email protected] |
124 | [email protected] "crate" | 124 | [email protected] |
125 | [email protected] "::" | 125 | [email protected] "root" |
126 | [email protected] | 126 | [email protected] "::" |
127 | [email protected] | 127 | [email protected] |
128 | [email protected] "export" | 128 | [email protected] |
129 | [email protected] "}" | 129 | [email protected] "export" |
130 | [email protected] "}" | 130 | [email protected] "}" |
131 | [email protected] "}" | 131 | [email protected] "}" |
132 | [email protected] ";" | 132 | [email protected] "}" |
133 | [email protected] " " | 133 | [email protected] ";" |
134 | [email protected] "// Nonsensical but pe ..." | 134 | [email protected] " " |
135 | [email protected] "\n" | 135 | [email protected] "// Nonsensical but pe ..." |
136 | [email protected] "\n" | ||
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rs b/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rs index 06c387cee..381cba1e2 100644 --- a/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rs +++ b/crates/ra_syntax/test_data/parser/inline/ok/0002_use_tree_list.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use {crate::path::from::root, or::path::from::crate_name}; // Rust 2018 (with a crate named `or`) | 1 | use {crate::path::from::root, or::path::from::crate_name}; // Rust 2018 (with a crate named `or`) |
2 | use {path::from::root}; // Rust 2015 | 2 | use {path::from::root}; // Rust 2015 |
3 | use ::{some::arbritrary::path}; // Rust 2015 | 3 | use ::{some::arbritrary::path}; // Rust 2015 |
4 | use ::{{{crate::export}}}; // Nonsensical but perfectly legal nestnig | 4 | use ::{{{root::export}}}; // Nonsensical but perfectly legal nesting |
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rast b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rast new file mode 100644 index 000000000..beb6d8010 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rast | |||
@@ -0,0 +1,35 @@ | |||
1 | [email protected] | ||
2 | [email protected] | ||
3 | [email protected] "fn" | ||
4 | [email protected] " " | ||
5 | [email protected] | ||
6 | [email protected] "foo" | ||
7 | [email protected] | ||
8 | [email protected] "(" | ||
9 | [email protected] ")" | ||
10 | [email protected] " " | ||
11 | [email protected] | ||
12 | [email protected] | ||
13 | [email protected] "{" | ||
14 | [email protected] " " | ||
15 | [email protected] | ||
16 | [email protected] | ||
17 | [email protected] | ||
18 | [email protected] | ||
19 | [email protected] | ||
20 | [email protected] "try" | ||
21 | [email protected] "!" | ||
22 | [email protected] | ||
23 | [email protected] "(" | ||
24 | [email protected] "Ok" | ||
25 | [email protected] | ||
26 | [email protected] "(" | ||
27 | [email protected] | ||
28 | [email protected] "(" | ||
29 | [email protected] ")" | ||
30 | [email protected] ")" | ||
31 | [email protected] ")" | ||
32 | [email protected] ";" | ||
33 | [email protected] " " | ||
34 | [email protected] "}" | ||
35 | [email protected] "\n" | ||
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rs b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rs new file mode 100644 index 000000000..61a6b46a0 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rs | |||
@@ -0,0 +1 @@ | |||
fn foo() { try!(Ok(())); } | |||
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0160_try_macro_rules.rast b/crates/ra_syntax/test_data/parser/inline/ok/0160_try_macro_rules.rast new file mode 100644 index 000000000..05b89d1c3 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0160_try_macro_rules.rast | |||
@@ -0,0 +1,27 @@ | |||
1 | [email protected] | ||
2 | [email protected] | ||
3 | [email protected] | ||
4 | [email protected] | ||
5 | [email protected] | ||
6 | [email protected] "macro_rules" | ||
7 | [email protected] "!" | ||
8 | [email protected] " " | ||
9 | [email protected] | ||
10 | [email protected] "try" | ||
11 | [email protected] " " | ||
12 | [email protected] | ||
13 | [email protected] "{" | ||
14 | [email protected] " " | ||
15 | [email protected] | ||
16 | [email protected] "(" | ||
17 | [email protected] ")" | ||
18 | [email protected] " " | ||
19 | [email protected] "=" | ||
20 | [email protected] ">" | ||
21 | [email protected] " " | ||
22 | [email protected] | ||
23 | [email protected] "{" | ||
24 | [email protected] "}" | ||
25 | [email protected] " " | ||
26 | [email protected] "}" | ||
27 | [email protected] "\n" | ||
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0160_try_macro_rules.rs b/crates/ra_syntax/test_data/parser/inline/ok/0160_try_macro_rules.rs new file mode 100644 index 000000000..2e2ab6e60 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0160_try_macro_rules.rs | |||
@@ -0,0 +1 @@ | |||
macro_rules! try { () => {} } | |||
diff --git a/docs/user/features.md b/docs/user/features.md index 56d2969fd..b9a365fc1 100644 --- a/docs/user/features.md +++ b/docs/user/features.md | |||
@@ -140,8 +140,8 @@ space or `;` depending on the return type of the function. | |||
140 | When completing a function call, `()` are automatically inserted. If a function | 140 | When completing a function call, `()` are automatically inserted. If a function |
141 | takes arguments, the cursor is positioned inside the parenthesis. | 141 | takes arguments, the cursor is positioned inside the parenthesis. |
142 | 142 | ||
143 | There are postifx completions, which can be triggerd by typing something like | 143 | There are postfix completions, which can be triggered by typing something like |
144 | `foo().if`. The word after `.` determines postifx completion. Possible variants are: | 144 | `foo().if`. The word after `.` determines postfix completion. Possible variants are: |
145 | 145 | ||
146 | - `expr.if` -> `if expr {}` | 146 | - `expr.if` -> `if expr {}` |
147 | - `expr.match` -> `match expr {}` | 147 | - `expr.match` -> `match expr {}` |
diff --git a/docs/user/readme.adoc b/docs/user/readme.adoc index 8b80a7df7..54342026b 100644 --- a/docs/user/readme.adoc +++ b/docs/user/readme.adoc | |||
@@ -169,13 +169,15 @@ The are several LSP client implementations for vim: | |||
169 | 169 | ||
170 | 1. Install coc.nvim by following the instructions at | 170 | 1. Install coc.nvim by following the instructions at |
171 | https://github.com/neoclide/coc.nvim[coc.nvim] | 171 | https://github.com/neoclide/coc.nvim[coc.nvim] |
172 | (nodejs required) | 172 | (Node.js required) |
173 | 2. Run `:CocInstall coc-rust-analyzer` to install | 173 | 2. Run `:CocInstall coc-rust-analyzer` to install |
174 | https://github.com/fannheyward/coc-rust-analyzer[coc-rust-analyzer], | 174 | https://github.com/fannheyward/coc-rust-analyzer[coc-rust-analyzer], |
175 | this extension implements _most_ of the features supported in the VSCode extension: | 175 | this extension implements _most_ of the features supported in the VSCode extension: |
176 | * automatically install and upgrade stable/nightly releases | ||
176 | * same configurations as VSCode extension, `rust-analyzer.serverPath`, `rust-analyzer.cargo.features` etc. | 177 | * same configurations as VSCode extension, `rust-analyzer.serverPath`, `rust-analyzer.cargo.features` etc. |
177 | * same commands too, `rust-analyzer.analyzerStatus`, `rust-analyzer.ssr` etc. | 178 | * same commands too, `rust-analyzer.analyzerStatus`, `rust-analyzer.ssr` etc. |
178 | * highlighting and inlay_hints are not implemented yet | 179 | * inlay hints for method chaining support, _Neovim Only_ |
180 | * semantic highlighting is not implemented yet | ||
179 | 181 | ||
180 | ==== LanguageClient-neovim | 182 | ==== LanguageClient-neovim |
181 | 183 | ||
diff --git a/editors/code/package.json b/editors/code/package.json index c4dfa7e13..d30673791 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -389,6 +389,28 @@ | |||
389 | "description": "Enable Proc macro support, cargo.loadOutDirsFromCheck must be enabled.", | 389 | "description": "Enable Proc macro support, cargo.loadOutDirsFromCheck must be enabled.", |
390 | "type": "boolean", | 390 | "type": "boolean", |
391 | "default": false | 391 | "default": false |
392 | }, | ||
393 | "rust-analyzer.debug.engine": { | ||
394 | "type": "string", | ||
395 | "enum": [ | ||
396 | "auto", | ||
397 | "vadimcn.vscode-lldb", | ||
398 | "ms-vscode.cpptools" | ||
399 | ], | ||
400 | "default": "auto", | ||
401 | "description": "Preffered debug engine.", | ||
402 | "markdownEnumDescriptions": [ | ||
403 | "First try to use [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb), if it's not installed try to use [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools).", | ||
404 | "Use [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)", | ||
405 | "Use [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)" | ||
406 | ] | ||
407 | }, | ||
408 | "rust-analyzer.debug.sourceFileMap": { | ||
409 | "type": "object", | ||
410 | "description": "Optional source file mappings passed to the debug engine.", | ||
411 | "default": { | ||
412 | "/rustc/<id>": "${env:USERPROFILE}/.rustup/toolchains/<toolchain-id>/lib/rustlib/src/rust" | ||
413 | } | ||
392 | } | 414 | } |
393 | } | 415 | } |
394 | }, | 416 | }, |
diff --git a/editors/code/src/cargo.ts b/editors/code/src/cargo.ts new file mode 100644 index 000000000..a328ba9bd --- /dev/null +++ b/editors/code/src/cargo.ts | |||
@@ -0,0 +1,106 @@ | |||
1 | import * as cp from 'child_process'; | ||
2 | import * as readline from 'readline'; | ||
3 | import { OutputChannel } from 'vscode'; | ||
4 | |||
5 | interface CompilationArtifact { | ||
6 | fileName: string; | ||
7 | name: string; | ||
8 | kind: string; | ||
9 | isTest: boolean; | ||
10 | } | ||
11 | |||
12 | export class Cargo { | ||
13 | rootFolder: string; | ||
14 | env?: Record<string, string>; | ||
15 | output: OutputChannel; | ||
16 | |||
17 | public constructor(cargoTomlFolder: string, output: OutputChannel, env: Record<string, string> | undefined = undefined) { | ||
18 | this.rootFolder = cargoTomlFolder; | ||
19 | this.output = output; | ||
20 | this.env = env; | ||
21 | } | ||
22 | |||
23 | public async artifactsFromArgs(cargoArgs: string[]): Promise<CompilationArtifact[]> { | ||
24 | const artifacts: CompilationArtifact[] = []; | ||
25 | |||
26 | try { | ||
27 | await this.runCargo(cargoArgs, | ||
28 | message => { | ||
29 | if (message.reason === 'compiler-artifact' && message.executable) { | ||
30 | const isBinary = message.target.crate_types.includes('bin'); | ||
31 | const isBuildScript = message.target.kind.includes('custom-build'); | ||
32 | if ((isBinary && !isBuildScript) || message.profile.test) { | ||
33 | artifacts.push({ | ||
34 | fileName: message.executable, | ||
35 | name: message.target.name, | ||
36 | kind: message.target.kind[0], | ||
37 | isTest: message.profile.test | ||
38 | }); | ||
39 | } | ||
40 | } | ||
41 | else if (message.reason === 'compiler-message') { | ||
42 | this.output.append(message.message.rendered); | ||
43 | } | ||
44 | }, | ||
45 | stderr => { | ||
46 | this.output.append(stderr); | ||
47 | } | ||
48 | ); | ||
49 | } | ||
50 | catch (err) { | ||
51 | this.output.show(true); | ||
52 | throw new Error(`Cargo invocation has failed: ${err}`); | ||
53 | } | ||
54 | |||
55 | return artifacts; | ||
56 | } | ||
57 | |||
58 | public async executableFromArgs(args: string[]): Promise<string> { | ||
59 | const cargoArgs = [...args]; // to remain args unchanged | ||
60 | cargoArgs.push("--message-format=json"); | ||
61 | |||
62 | const artifacts = await this.artifactsFromArgs(cargoArgs); | ||
63 | |||
64 | if (artifacts.length === 0) { | ||
65 | throw new Error('No compilation artifacts'); | ||
66 | } else if (artifacts.length > 1) { | ||
67 | throw new Error('Multiple compilation artifacts are not supported.'); | ||
68 | } | ||
69 | |||
70 | return artifacts[0].fileName; | ||
71 | } | ||
72 | |||
73 | runCargo( | ||
74 | cargoArgs: string[], | ||
75 | onStdoutJson: (obj: any) => void, | ||
76 | onStderrString: (data: string) => void | ||
77 | ): Promise<number> { | ||
78 | return new Promise<number>((resolve, reject) => { | ||
79 | const cargo = cp.spawn('cargo', cargoArgs, { | ||
80 | stdio: ['ignore', 'pipe', 'pipe'], | ||
81 | cwd: this.rootFolder, | ||
82 | env: this.env, | ||
83 | }); | ||
84 | |||
85 | cargo.on('error', err => { | ||
86 | reject(new Error(`could not launch cargo: ${err}`)); | ||
87 | }); | ||
88 | cargo.stderr.on('data', chunk => { | ||
89 | onStderrString(chunk.toString()); | ||
90 | }); | ||
91 | |||
92 | const rl = readline.createInterface({ input: cargo.stdout }); | ||
93 | rl.on('line', line => { | ||
94 | const message = JSON.parse(line); | ||
95 | onStdoutJson(message); | ||
96 | }); | ||
97 | |||
98 | cargo.on('exit', (exitCode, _) => { | ||
99 | if (exitCode === 0) | ||
100 | resolve(exitCode); | ||
101 | else | ||
102 | reject(new Error(`exit code: ${exitCode}.`)); | ||
103 | }); | ||
104 | }); | ||
105 | } | ||
106 | } \ No newline at end of file | ||
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 2635a1440..d77e8188c 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts | |||
@@ -1,8 +1,10 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
3 | import * as ra from '../rust-analyzer-api'; | 3 | import * as ra from '../rust-analyzer-api'; |
4 | import * as os from "os"; | ||
4 | 5 | ||
5 | import { Ctx, Cmd } from '../ctx'; | 6 | import { Ctx, Cmd } from '../ctx'; |
7 | import { Cargo } from '../cargo'; | ||
6 | 8 | ||
7 | export function run(ctx: Ctx): Cmd { | 9 | export function run(ctx: Ctx): Cmd { |
8 | let prevRunnable: RunnableQuickPick | undefined; | 10 | let prevRunnable: RunnableQuickPick | undefined; |
@@ -62,25 +64,69 @@ export function runSingle(ctx: Ctx): Cmd { | |||
62 | }; | 64 | }; |
63 | } | 65 | } |
64 | 66 | ||
67 | function getLldbDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): vscode.DebugConfiguration { | ||
68 | return { | ||
69 | type: "lldb", | ||
70 | request: "launch", | ||
71 | name: config.label, | ||
72 | cargo: { | ||
73 | args: config.args, | ||
74 | }, | ||
75 | args: config.extraArgs, | ||
76 | cwd: config.cwd, | ||
77 | sourceMap: sourceFileMap | ||
78 | }; | ||
79 | } | ||
80 | |||
81 | const debugOutput = vscode.window.createOutputChannel("Debug"); | ||
82 | |||
83 | async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): Promise<vscode.DebugConfiguration> { | ||
84 | debugOutput.clear(); | ||
85 | |||
86 | const cargo = new Cargo(config.cwd || '.', debugOutput); | ||
87 | const executable = await cargo.executableFromArgs(config.args); | ||
88 | |||
89 | // if we are here, there were no compilation errors. | ||
90 | return { | ||
91 | type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg', | ||
92 | request: "launch", | ||
93 | name: config.label, | ||
94 | program: executable, | ||
95 | args: config.extraArgs, | ||
96 | cwd: config.cwd, | ||
97 | sourceFileMap: sourceFileMap, | ||
98 | }; | ||
99 | } | ||
100 | |||
65 | export function debugSingle(ctx: Ctx): Cmd { | 101 | export function debugSingle(ctx: Ctx): Cmd { |
66 | return async (config: ra.Runnable) => { | 102 | return async (config: ra.Runnable) => { |
67 | const editor = ctx.activeRustEditor; | 103 | const editor = ctx.activeRustEditor; |
68 | if (!editor) return; | 104 | if (!editor) return; |
69 | if (!vscode.extensions.getExtension("vadimcn.vscode-lldb")) { | 105 | |
70 | vscode.window.showErrorMessage("Install `vadimcn.vscode-lldb` extension for debugging"); | 106 | const lldbId = "vadimcn.vscode-lldb"; |
107 | const cpptoolsId = "ms-vscode.cpptools"; | ||
108 | |||
109 | const debugEngineId = ctx.config.debug.engine; | ||
110 | let debugEngine = null; | ||
111 | if (debugEngineId === "auto") { | ||
112 | debugEngine = vscode.extensions.getExtension(lldbId); | ||
113 | if (!debugEngine) { | ||
114 | debugEngine = vscode.extensions.getExtension(cpptoolsId); | ||
115 | } | ||
116 | } | ||
117 | else { | ||
118 | debugEngine = vscode.extensions.getExtension(debugEngineId); | ||
119 | } | ||
120 | |||
121 | if (!debugEngine) { | ||
122 | vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=${lldbId})` | ||
123 | + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=${cpptoolsId}) extension for debugging.`); | ||
71 | return; | 124 | return; |
72 | } | 125 | } |
73 | 126 | ||
74 | const debugConfig = { | 127 | const debugConfig = lldbId === debugEngine.id |
75 | type: "lldb", | 128 | ? getLldbDebugConfig(config, ctx.config.debug.sourceFileMap) |
76 | request: "launch", | 129 | : await getCppvsDebugConfig(config, ctx.config.debug.sourceFileMap); |
77 | name: config.label, | ||
78 | cargo: { | ||
79 | args: config.args, | ||
80 | }, | ||
81 | args: config.extraArgs, | ||
82 | cwd: config.cwd | ||
83 | }; | ||
84 | 130 | ||
85 | return vscode.debug.startDebugging(undefined, debugConfig); | 131 | return vscode.debug.startDebugging(undefined, debugConfig); |
86 | }; | 132 | }; |
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 3b2eec8ba..110e54180 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts | |||
@@ -92,7 +92,6 @@ export class Config { | |||
92 | get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); } | 92 | get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); } |
93 | get traceExtension() { return this.get<boolean>("trace.extension"); } | 93 | get traceExtension() { return this.get<boolean>("trace.extension"); } |
94 | 94 | ||
95 | |||
96 | get inlayHints() { | 95 | get inlayHints() { |
97 | return { | 96 | return { |
98 | typeHints: this.get<boolean>("inlayHints.typeHints"), | 97 | typeHints: this.get<boolean>("inlayHints.typeHints"), |
@@ -107,4 +106,12 @@ export class Config { | |||
107 | command: this.get<string>("checkOnSave.command"), | 106 | command: this.get<string>("checkOnSave.command"), |
108 | }; | 107 | }; |
109 | } | 108 | } |
109 | |||
110 | get debug() { | ||
111 | return { | ||
112 | engine: this.get<string>("debug.engine"), | ||
113 | sourceFileMap: this.get<Record<string, string>>("debug.sourceFileMap"), | ||
114 | }; | ||
115 | } | ||
116 | |||
110 | } | 117 | } |
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs index 9c02f7c6f..98c8644e4 100644 --- a/xtask/src/ast_src.rs +++ b/xtask/src/ast_src.rs | |||
@@ -595,7 +595,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { | |||
595 | qualifier: Path, | 595 | qualifier: Path, |
596 | } | 596 | } |
597 | struct PathSegment { | 597 | struct PathSegment { |
598 | T![::], T![<], NameRef, TypeArgList, ParamList, RetType, PathType, T![>] | 598 | T![::], T![crate], T![<], NameRef, TypeArgList, ParamList, RetType, PathType, T![>] |
599 | } | 599 | } |
600 | struct TypeArgList { | 600 | struct TypeArgList { |
601 | T![::], | 601 | T![::], |
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index ec824a518..2b7a461e5 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs | |||
@@ -10,23 +10,19 @@ pub mod pre_commit; | |||
10 | pub mod codegen; | 10 | pub mod codegen; |
11 | mod ast_src; | 11 | mod ast_src; |
12 | 12 | ||
13 | use anyhow::Context; | ||
14 | use std::{ | 13 | use std::{ |
15 | env, | 14 | env, |
16 | io::Write, | ||
17 | path::{Path, PathBuf}, | 15 | path::{Path, PathBuf}, |
18 | process::{Command, Stdio}, | ||
19 | }; | 16 | }; |
17 | |||
20 | use walkdir::{DirEntry, WalkDir}; | 18 | use walkdir::{DirEntry, WalkDir}; |
21 | 19 | ||
22 | use crate::{ | 20 | use crate::{ |
23 | codegen::Mode, | 21 | codegen::Mode, |
24 | not_bash::{date_iso, fs2, pushd, rm_rf, run}, | 22 | not_bash::{date_iso, fs2, pushd, pushenv, rm_rf, run}, |
25 | }; | 23 | }; |
26 | 24 | ||
27 | pub use anyhow::Result; | 25 | pub use anyhow::{bail, Context as _, Result}; |
28 | |||
29 | const TOOLCHAIN: &str = "stable"; | ||
30 | 26 | ||
31 | pub fn project_root() -> PathBuf { | 27 | pub fn project_root() -> PathBuf { |
32 | Path::new( | 28 | Path::new( |
@@ -55,54 +51,44 @@ pub fn rust_files(path: &Path) -> impl Iterator<Item = PathBuf> { | |||
55 | 51 | ||
56 | pub fn run_rustfmt(mode: Mode) -> Result<()> { | 52 | pub fn run_rustfmt(mode: Mode) -> Result<()> { |
57 | let _dir = pushd(project_root()); | 53 | let _dir = pushd(project_root()); |
54 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
58 | ensure_rustfmt()?; | 55 | ensure_rustfmt()?; |
59 | 56 | match mode { | |
60 | let check = if mode == Mode::Verify { "--check" } else { "" }; | 57 | Mode::Overwrite => run!("cargo fmt"), |
61 | run!("rustup run {} -- cargo fmt -- {}", TOOLCHAIN, check)?; | 58 | Mode::Verify => run!("cargo fmt -- --check"), |
59 | }?; | ||
62 | Ok(()) | 60 | Ok(()) |
63 | } | 61 | } |
64 | 62 | ||
65 | fn reformat(text: impl std::fmt::Display) -> Result<String> { | 63 | fn reformat(text: impl std::fmt::Display) -> Result<String> { |
64 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
66 | ensure_rustfmt()?; | 65 | ensure_rustfmt()?; |
67 | let mut rustfmt = Command::new("rustup") | 66 | let stdout = run!( |
68 | .args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"]) | 67 | "rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display(); |
69 | .arg(project_root().join("rustfmt.toml")) | 68 | <text.to_string().as_bytes() |
70 | .args(&["--config", "fn_single_line=true"]) | 69 | )?; |
71 | .stdin(Stdio::piped()) | ||
72 | .stdout(Stdio::piped()) | ||
73 | .spawn()?; | ||
74 | write!(rustfmt.stdin.take().unwrap(), "{}", text)?; | ||
75 | let output = rustfmt.wait_with_output()?; | ||
76 | let stdout = String::from_utf8(output.stdout)?; | ||
77 | let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`"; | 70 | let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`"; |
78 | Ok(format!("//! {}\n\n{}", preamble, stdout)) | 71 | Ok(format!("//! {}\n\n{}\n", preamble, stdout)) |
79 | } | 72 | } |
80 | 73 | ||
81 | fn ensure_rustfmt() -> Result<()> { | 74 | fn ensure_rustfmt() -> Result<()> { |
82 | match Command::new("rustup") | 75 | let out = run!("rustfmt --version")?; |
83 | .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) | 76 | if !out.contains("stable") { |
84 | .stderr(Stdio::null()) | 77 | bail!( |
85 | .stdout(Stdio::null()) | 78 | "Failed to run rustfmt from toolchain 'stable'. \ |
86 | .status() | 79 | Please run `rustup component add rustfmt --toolchain stable` to install it.", |
87 | { | 80 | ) |
88 | Ok(status) if status.success() => return Ok(()), | 81 | } |
89 | _ => (), | ||
90 | }; | ||
91 | run!("rustup toolchain install {}", TOOLCHAIN)?; | ||
92 | run!("rustup component add rustfmt --toolchain {}", TOOLCHAIN)?; | ||
93 | Ok(()) | 82 | Ok(()) |
94 | } | 83 | } |
95 | 84 | ||
96 | pub fn run_clippy() -> Result<()> { | 85 | pub fn run_clippy() -> Result<()> { |
97 | match Command::new("rustup") | 86 | if run!("cargo clippy --version").is_err() { |
98 | .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) | 87 | bail!( |
99 | .stderr(Stdio::null()) | 88 | "Failed run cargo clippy. \ |
100 | .stdout(Stdio::null()) | 89 | Please run `rustup component add clippy` to install it.", |
101 | .status() | 90 | ) |
102 | { | 91 | } |
103 | Ok(status) if status.success() => (), | ||
104 | _ => install_clippy().context("install clippy")?, | ||
105 | }; | ||
106 | 92 | ||
107 | let allowed_lints = [ | 93 | let allowed_lints = [ |
108 | "clippy::collapsible_if", | 94 | "clippy::collapsible_if", |
@@ -110,27 +96,24 @@ pub fn run_clippy() -> Result<()> { | |||
110 | "clippy::nonminimal_bool", | 96 | "clippy::nonminimal_bool", |
111 | "clippy::redundant_pattern_matching", | 97 | "clippy::redundant_pattern_matching", |
112 | ]; | 98 | ]; |
113 | run!( | 99 | run!("cargo clippy --all-features --all-targets -- -A {}", allowed_lints.join(" -A "))?; |
114 | "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}", | ||
115 | TOOLCHAIN, | ||
116 | allowed_lints.join(" -A ") | ||
117 | )?; | ||
118 | Ok(()) | ||
119 | } | ||
120 | |||
121 | fn install_clippy() -> Result<()> { | ||
122 | run!("rustup toolchain install {}", TOOLCHAIN)?; | ||
123 | run!("rustup component add clippy --toolchain {}", TOOLCHAIN)?; | ||
124 | Ok(()) | 100 | Ok(()) |
125 | } | 101 | } |
126 | 102 | ||
127 | pub fn run_fuzzer() -> Result<()> { | 103 | pub fn run_fuzzer() -> Result<()> { |
128 | let _d = pushd("./crates/ra_syntax"); | 104 | let _d = pushd("./crates/ra_syntax"); |
105 | let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly"); | ||
129 | if run!("cargo fuzz --help").is_err() { | 106 | if run!("cargo fuzz --help").is_err() { |
130 | run!("cargo install cargo-fuzz")?; | 107 | run!("cargo install cargo-fuzz")?; |
131 | }; | 108 | }; |
132 | 109 | ||
133 | run!("rustup run nightly -- cargo fuzz run parser")?; | 110 | // Expecting nightly rustc |
111 | let out = run!("rustc --version")?; | ||
112 | if !out.contains("nightly") { | ||
113 | bail!("fuzz tests require nightly rustc") | ||
114 | } | ||
115 | |||
116 | run!("cargo fuzz run parser")?; | ||
134 | Ok(()) | 117 | Ok(()) |
135 | } | 118 | } |
136 | 119 | ||
diff --git a/xtask/src/not_bash.rs b/xtask/src/not_bash.rs index ef1699934..a6431e586 100644 --- a/xtask/src/not_bash.rs +++ b/xtask/src/not_bash.rs | |||
@@ -3,6 +3,8 @@ | |||
3 | use std::{ | 3 | use std::{ |
4 | cell::RefCell, | 4 | cell::RefCell, |
5 | env, | 5 | env, |
6 | ffi::OsString, | ||
7 | io::Write, | ||
6 | path::{Path, PathBuf}, | 8 | path::{Path, PathBuf}, |
7 | process::{Command, Stdio}, | 9 | process::{Command, Stdio}, |
8 | }; | 10 | }; |
@@ -57,7 +59,10 @@ macro_rules! _run { | |||
57 | run!($($expr),*; echo = true) | 59 | run!($($expr),*; echo = true) |
58 | }; | 60 | }; |
59 | ($($expr:expr),* ; echo = $echo:expr) => { | 61 | ($($expr:expr),* ; echo = $echo:expr) => { |
60 | $crate::not_bash::run_process(format!($($expr),*), $echo) | 62 | $crate::not_bash::run_process(format!($($expr),*), $echo, None) |
63 | }; | ||
64 | ($($expr:expr),* ; <$stdin:expr) => { | ||
65 | $crate::not_bash::run_process(format!($($expr),*), false, Some($stdin)) | ||
61 | }; | 66 | }; |
62 | } | 67 | } |
63 | pub(crate) use _run as run; | 68 | pub(crate) use _run as run; |
@@ -77,6 +82,21 @@ impl Drop for Pushd { | |||
77 | } | 82 | } |
78 | } | 83 | } |
79 | 84 | ||
85 | pub struct Pushenv { | ||
86 | _p: (), | ||
87 | } | ||
88 | |||
89 | pub fn pushenv(var: &str, value: &str) -> Pushenv { | ||
90 | Env::with(|env| env.pushenv(var.into(), value.into())); | ||
91 | Pushenv { _p: () } | ||
92 | } | ||
93 | |||
94 | impl Drop for Pushenv { | ||
95 | fn drop(&mut self) { | ||
96 | Env::with(|env| env.popenv()) | ||
97 | } | ||
98 | } | ||
99 | |||
80 | pub fn rm_rf(path: impl AsRef<Path>) -> Result<()> { | 100 | pub fn rm_rf(path: impl AsRef<Path>) -> Result<()> { |
81 | let path = path.as_ref(); | 101 | let path = path.as_ref(); |
82 | if !path.exists() { | 102 | if !path.exists() { |
@@ -90,15 +110,15 @@ pub fn rm_rf(path: impl AsRef<Path>) -> Result<()> { | |||
90 | } | 110 | } |
91 | 111 | ||
92 | #[doc(hidden)] | 112 | #[doc(hidden)] |
93 | pub fn run_process(cmd: String, echo: bool) -> Result<String> { | 113 | pub fn run_process(cmd: String, echo: bool, stdin: Option<&[u8]>) -> Result<String> { |
94 | run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd)) | 114 | run_process_inner(&cmd, echo, stdin).with_context(|| format!("process `{}` failed", cmd)) |
95 | } | 115 | } |
96 | 116 | ||
97 | pub fn date_iso() -> Result<String> { | 117 | pub fn date_iso() -> Result<String> { |
98 | run!("date --iso --utc") | 118 | run!("date --iso --utc") |
99 | } | 119 | } |
100 | 120 | ||
101 | fn run_process_inner(cmd: &str, echo: bool) -> Result<String> { | 121 | fn run_process_inner(cmd: &str, echo: bool, stdin: Option<&[u8]>) -> Result<String> { |
102 | let mut args = shelx(cmd); | 122 | let mut args = shelx(cmd); |
103 | let binary = args.remove(0); | 123 | let binary = args.remove(0); |
104 | let current_dir = Env::with(|it| it.cwd().to_path_buf()); | 124 | let current_dir = Env::with(|it| it.cwd().to_path_buf()); |
@@ -107,12 +127,17 @@ fn run_process_inner(cmd: &str, echo: bool) -> Result<String> { | |||
107 | println!("> {}", cmd) | 127 | println!("> {}", cmd) |
108 | } | 128 | } |
109 | 129 | ||
110 | let output = Command::new(binary) | 130 | let mut command = Command::new(binary); |
111 | .args(args) | 131 | command.args(args).current_dir(current_dir).stderr(Stdio::inherit()); |
112 | .current_dir(current_dir) | 132 | let output = match stdin { |
113 | .stdin(Stdio::null()) | 133 | None => command.stdin(Stdio::null()).output(), |
114 | .stderr(Stdio::inherit()) | 134 | Some(stdin) => { |
115 | .output()?; | 135 | command.stdin(Stdio::piped()).stdout(Stdio::piped()); |
136 | let mut process = command.spawn()?; | ||
137 | process.stdin.take().unwrap().write_all(stdin)?; | ||
138 | process.wait_with_output() | ||
139 | } | ||
140 | }?; | ||
116 | let stdout = String::from_utf8(output.stdout)?; | 141 | let stdout = String::from_utf8(output.stdout)?; |
117 | 142 | ||
118 | if echo { | 143 | if echo { |
@@ -133,13 +158,15 @@ fn shelx(cmd: &str) -> Vec<String> { | |||
133 | 158 | ||
134 | struct Env { | 159 | struct Env { |
135 | pushd_stack: Vec<PathBuf>, | 160 | pushd_stack: Vec<PathBuf>, |
161 | pushenv_stack: Vec<(OsString, Option<OsString>)>, | ||
136 | } | 162 | } |
137 | 163 | ||
138 | impl Env { | 164 | impl Env { |
139 | fn with<F: FnOnce(&mut Env) -> T, T>(f: F) -> T { | 165 | fn with<F: FnOnce(&mut Env) -> T, T>(f: F) -> T { |
140 | thread_local! { | 166 | thread_local! { |
141 | static ENV: RefCell<Env> = RefCell::new(Env { | 167 | static ENV: RefCell<Env> = RefCell::new(Env { |
142 | pushd_stack: vec![env::current_dir().unwrap()] | 168 | pushd_stack: vec![env::current_dir().unwrap()], |
169 | pushenv_stack: vec![], | ||
143 | }); | 170 | }); |
144 | } | 171 | } |
145 | ENV.with(|it| f(&mut *it.borrow_mut())) | 172 | ENV.with(|it| f(&mut *it.borrow_mut())) |
@@ -154,6 +181,17 @@ impl Env { | |||
154 | self.pushd_stack.pop().unwrap(); | 181 | self.pushd_stack.pop().unwrap(); |
155 | env::set_current_dir(self.cwd()).unwrap(); | 182 | env::set_current_dir(self.cwd()).unwrap(); |
156 | } | 183 | } |
184 | fn pushenv(&mut self, var: OsString, value: OsString) { | ||
185 | self.pushenv_stack.push((var.clone(), env::var_os(&var))); | ||
186 | env::set_var(var, value) | ||
187 | } | ||
188 | fn popenv(&mut self) { | ||
189 | let (var, value) = self.pushenv_stack.pop().unwrap(); | ||
190 | match value { | ||
191 | None => env::remove_var(var), | ||
192 | Some(value) => env::set_var(var, value), | ||
193 | } | ||
194 | } | ||
157 | fn cwd(&self) -> &Path { | 195 | fn cwd(&self) -> &Path { |
158 | self.pushd_stack.last().unwrap() | 196 | self.pushd_stack.last().unwrap() |
159 | } | 197 | } |