Skip to content

Commit

Permalink
Auto merge of #17243 - Veykril:static-lt-hir, r=Veykril
Browse files Browse the repository at this point in the history
internal: Add StaticLifetime to hir API
  • Loading branch information
bors committed May 16, 2024
2 parents 65dbe8e + b7e194e commit 5706217
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 40 deletions.
9 changes: 9 additions & 0 deletions src/tools/rust-analyzer/crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,15 @@ impl HasVisibility for TypeAlias {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StaticLifetime;

impl StaticLifetime {
pub fn name(self) -> Name {
known::STATIC_LIFETIME
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BuiltinType {
pub(crate) inner: hir_def::builtin_type::BuiltinType,
Expand Down
15 changes: 12 additions & 3 deletions src/tools/rust-analyzer/crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use hir::{
Adt, AsAssocItem, AsExternAssocItem, AssocItem, AttributeTemplate, BuiltinAttr, BuiltinType,
Const, Crate, DefWithBody, DeriveHelper, DocLinkDef, ExternAssocItem, ExternCrateDecl, Field,
Function, GenericParam, HasVisibility, HirDisplay, Impl, Label, Local, Macro, Module,
ModuleDef, Name, PathResolution, Semantics, Static, ToolModule, Trait, TraitAlias, TupleField,
TypeAlias, Variant, VariantDef, Visibility,
ModuleDef, Name, PathResolution, Semantics, Static, StaticLifetime, ToolModule, Trait,
TraitAlias, TupleField, TypeAlias, Variant, VariantDef, Visibility,
};
use stdx::{format_to, impl_from};
use syntax::{
Expand All @@ -39,12 +39,13 @@ pub enum Definition {
Trait(Trait),
TraitAlias(TraitAlias),
TypeAlias(TypeAlias),
BuiltinType(BuiltinType),
SelfType(Impl),
GenericParam(GenericParam),
Local(Local),
Label(Label),
DeriveHelper(DeriveHelper),
BuiltinType(BuiltinType),
BuiltinLifetime(StaticLifetime),
BuiltinAttr(BuiltinAttr),
ToolModule(ToolModule),
ExternCrateDecl(ExternCrateDecl),
Expand Down Expand Up @@ -83,6 +84,7 @@ impl Definition {
Definition::DeriveHelper(it) => it.derive().module(db),
Definition::BuiltinAttr(_)
| Definition::BuiltinType(_)
| Definition::BuiltinLifetime(_)
| Definition::TupleField(_)
| Definition::ToolModule(_) => return None,
};
Expand Down Expand Up @@ -112,6 +114,7 @@ impl Definition {
Definition::BuiltinType(_) | Definition::TupleField(_) => Visibility::Public,
Definition::Macro(_) => return None,
Definition::BuiltinAttr(_)
| Definition::BuiltinLifetime(_)
| Definition::ToolModule(_)
| Definition::SelfType(_)
| Definition::Local(_)
Expand Down Expand Up @@ -141,6 +144,7 @@ impl Definition {
Definition::Local(it) => it.name(db),
Definition::GenericParam(it) => it.name(db),
Definition::Label(it) => it.name(db),
Definition::BuiltinLifetime(StaticLifetime) => hir::known::STATIC_LIFETIME,
Definition::BuiltinAttr(_) => return None, // FIXME
Definition::ToolModule(_) => return None, // FIXME
Definition::DeriveHelper(it) => it.name(db),
Expand Down Expand Up @@ -174,6 +178,7 @@ impl Definition {
doc_owner.docs(fd.0.db)
})
}
Definition::BuiltinLifetime(StaticLifetime) => None,
Definition::Local(_) => None,
Definition::SelfType(impl_def) => {
impl_def.self_ty(db).as_adt().map(|adt| adt.docs(db))?
Expand Down Expand Up @@ -228,6 +233,7 @@ impl Definition {
Definition::TraitAlias(it) => it.display(db).to_string(),
Definition::TypeAlias(it) => it.display(db).to_string(),
Definition::BuiltinType(it) => it.name().display(db).to_string(),
Definition::BuiltinLifetime(it) => it.name().display(db).to_string(),
Definition::Local(it) => {
let ty = it.ty(db);
let ty_display = ty.display_truncated(db, None);
Expand Down Expand Up @@ -693,6 +699,9 @@ impl NameRefClass {
) -> Option<NameRefClass> {
let _p = tracing::span!(tracing::Level::INFO, "NameRefClass::classify_lifetime", ?lifetime)
.entered();
if lifetime.text() == "'static" {
return Some(NameRefClass::Definition(Definition::BuiltinLifetime(StaticLifetime)));
}
let parent = lifetime.syntax().parent()?;
match parent.kind() {
SyntaxKind::BREAK_EXPR | SyntaxKind::CONTINUE_EXPR => {
Expand Down
11 changes: 6 additions & 5 deletions src/tools/rust-analyzer/crates/ide-db/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,12 @@ impl Definition {
.and_then(syn_ctx_is_root)
}
}
Definition::BuiltinType(_) => return None,
Definition::SelfType(_) => return None,
Definition::BuiltinAttr(_) => return None,
Definition::ToolModule(_) => return None,
Definition::TupleField(_) => return None,
Definition::BuiltinType(_)
| Definition::BuiltinLifetime(_)
| Definition::BuiltinAttr(_)
| Definition::SelfType(_)
| Definition::ToolModule(_)
| Definition::TupleField(_) => return None,
// FIXME: This should be doable in theory
Definition::DeriveHelper(_) => return None,
};
Expand Down
4 changes: 3 additions & 1 deletion src/tools/rust-analyzer/crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ pub(crate) fn resolve_doc_path_for_def(
Definition::SelfType(it) => it.resolve_doc_path(db, link, ns),
Definition::ExternCrateDecl(it) => it.resolve_doc_path(db, link, ns),
Definition::BuiltinAttr(_)
| Definition::ToolModule(_)
| Definition::BuiltinType(_)
| Definition::BuiltinLifetime(_)
| Definition::ToolModule(_)
| Definition::TupleField(_)
| Definition::Local(_)
| Definition::GenericParam(_)
Expand Down Expand Up @@ -648,6 +649,7 @@ fn filename_and_frag_for_def(
| Definition::TupleField(_)
| Definition::Label(_)
| Definition::BuiltinAttr(_)
| Definition::BuiltinLifetime(_)
| Definition::ToolModule(_)
| Definition::DeriveHelper(_) => return None,
};
Expand Down
10 changes: 1 addition & 9 deletions src/tools/rust-analyzer/crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,15 +756,7 @@ fn closure_ty(
}

fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
if matches!(
def,
Definition::GenericParam(_)
| Definition::BuiltinType(_)
| Definition::Local(_)
| Definition::Label(_)
| Definition::BuiltinAttr(_)
| Definition::ToolModule(_)
) {
if matches!(def, Definition::GenericParam(_) | Definition::Local(_) | Definition::Label(_)) {
return None;
}
def.module(db).map(|module| path(db, module, definition_owner_name(db, def)))
Expand Down
14 changes: 12 additions & 2 deletions src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4441,7 +4441,7 @@ fn foo() {
```rust
'label
```
"#]],
"#]],
);
}

Expand All @@ -4455,7 +4455,17 @@ fn hover_lifetime() {
```rust
'lifetime
```
"#]],
"#]],
);
check(
r#"fn foo(_: &'static$0 ()) {}"#,
expect![[r#"
*'static*
```rust
'static
```
"#]],
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/tools/rust-analyzer/crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ pub(crate) fn def_to_kind(db: &RootDatabase, def: Definition) -> SymbolInformati
}
}
Definition::BuiltinType(..) => Type,
Definition::BuiltinLifetime(_) => TypeParameter,
Definition::SelfType(..) => TypeAlias,
Definition::GenericParam(..) => TypeParameter,
Definition::Local(it) => {
Expand Down Expand Up @@ -316,6 +317,7 @@ pub(crate) fn def_to_moniker(
Definition::GenericParam(_)
| Definition::Label(_)
| Definition::DeriveHelper(_)
| Definition::BuiltinLifetime(_)
| Definition::BuiltinAttr(_)
| Definition::ToolModule(_) => return None,

Expand Down
8 changes: 5 additions & 3 deletions src/tools/rust-analyzer/crates/ide/src/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,11 @@ impl TryToNav for Definition {
Definition::TraitAlias(it) => it.try_to_nav(db),
Definition::TypeAlias(it) => it.try_to_nav(db),
Definition::ExternCrateDecl(it) => Some(it.try_to_nav(db)?),
Definition::BuiltinType(_) | Definition::TupleField(_) => None,
Definition::ToolModule(_) => None,
Definition::BuiltinAttr(_) => None,
Definition::BuiltinLifetime(_)
| Definition::BuiltinType(_)
| Definition::TupleField(_)
| Definition::ToolModule(_)
| Definition::BuiltinAttr(_) => None,
// FIXME: The focus range should be set to the helper declaration
Definition::DeriveHelper(it) => it.derive().try_to_nav(db),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ pub(super) fn highlight_def(
h
}
Definition::BuiltinType(_) => Highlight::new(HlTag::BuiltinType),
Definition::BuiltinLifetime(_) => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)),
Definition::Static(s) => {
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));

Expand Down Expand Up @@ -542,13 +543,14 @@ pub(super) fn highlight_def(
let def_crate = def.krate(db);
let is_from_other_crate = def_crate != Some(krate);
let is_from_builtin_crate = def_crate.map_or(false, |def_crate| def_crate.is_builtin(db));
let is_builtin_type = matches!(def, Definition::BuiltinType(_));
let is_public = def.visibility(db) == Some(hir::Visibility::Public);

match (is_from_other_crate, is_builtin_type, is_public) {
(true, false, _) => h |= HlMod::Library,
(false, _, true) => h |= HlMod::Public,
_ => {}
let is_builtin = matches!(
def,
Definition::BuiltinType(_) | Definition::BuiltinLifetime(_) | Definition::BuiltinAttr(_)
);
match is_from_other_crate {
true if !is_builtin => h |= HlMod::Library,
false if def.visibility(db) == Some(hir::Visibility::Public) => h |= HlMod::Public,
_ => (),
}

if is_from_builtin_crate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ fn module_def_to_hl_tag(def: Definition) -> HlTag {
Definition::Trait(_) => SymbolKind::Trait,
Definition::TraitAlias(_) => SymbolKind::TraitAlias,
Definition::TypeAlias(_) => SymbolKind::TypeAlias,
Definition::BuiltinLifetime(_) => SymbolKind::LifetimeParam,
Definition::BuiltinType(_) => return HlTag::BuiltinType,
Definition::Macro(_) => SymbolKind::Macro,
Definition::Field(_) | Definition::TupleField(_) => SymbolKind::Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">allow</span><span class="parenthesis attribute">(</span><span class="none attribute">dead_code</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
<pre><code><span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">allow</span><span class="parenthesis attribute">(</span><span class="none attribute">dead_code</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="tool_module attribute library">rustfmt</span><span class="operator attribute">::</span><span class="tool_module attribute library">skip</span><span class="attribute_bracket attribute">]</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">identity</span><span class="attribute_bracket attribute">]</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="attribute attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="derive attribute default_library library">Default</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
Expand Down
Loading

0 comments on commit 5706217

Please sign in to comment.