Skip to content

Commit 11c0a5b

Browse files
committed
Highlight mutable statics as mutable
1 parent 4578154 commit 11c0a5b

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,10 @@ impl Static {
678678
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
679679
db.static_data(self.id).name.clone()
680680
}
681+
682+
pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
683+
db.static_data(self.id).mutable
684+
}
681685
}
682686

683687
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/ra_hir_def/src/data.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,6 @@ impl ConstData {
251251
Arc::new(ConstData::new(db, vis_default, node))
252252
}
253253

254-
pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<ConstData> {
255-
let node = konst.lookup(db).source(db);
256-
Arc::new(ConstData::new(db, RawVisibility::private(), node))
257-
}
258-
259254
fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>(
260255
db: &dyn DefDatabase,
261256
vis_default: RawVisibility,
@@ -270,6 +265,32 @@ impl ConstData {
270265
}
271266
}
272267

268+
#[derive(Debug, Clone, PartialEq, Eq)]
269+
pub struct StaticData {
270+
pub name: Option<Name>,
271+
pub type_ref: TypeRef,
272+
pub visibility: RawVisibility,
273+
pub mutable: bool,
274+
}
275+
276+
impl StaticData {
277+
pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<StaticData> {
278+
let node = konst.lookup(db).source(db);
279+
let ctx = LowerCtx::new(db, node.file_id);
280+
281+
let name = node.value.name().map(|n| n.as_name());
282+
let type_ref = TypeRef::from_ast_opt(&ctx, node.value.ascribed_type());
283+
let mutable = node.value.mut_token().is_some();
284+
let visibility = RawVisibility::from_ast_with_default(
285+
db,
286+
RawVisibility::private(),
287+
node.map(|n| n.visibility()),
288+
);
289+
290+
Arc::new(StaticData { name, type_ref, visibility, mutable })
291+
}
292+
}
293+
273294
fn collect_items_in_macros(
274295
db: &dyn DefDatabase,
275296
expander: &mut Expander,

crates/ra_hir_def/src/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
adt::{EnumData, StructData},
1111
attr::Attrs,
1212
body::{scope::ExprScopes, Body, BodySourceMap},
13-
data::{ConstData, FunctionData, ImplData, TraitData, TypeAliasData},
13+
data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
1414
docs::Documentation,
1515
generics::GenericParams,
1616
lang_item::{LangItemTarget, LangItems},
@@ -77,8 +77,8 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
7777
#[salsa::invoke(ConstData::const_data_query)]
7878
fn const_data(&self, konst: ConstId) -> Arc<ConstData>;
7979

80-
#[salsa::invoke(ConstData::static_data_query)]
81-
fn static_data(&self, konst: StaticId) -> Arc<ConstData>;
80+
#[salsa::invoke(StaticData::static_data_query)]
81+
fn static_data(&self, konst: StaticId) -> Arc<StaticData>;
8282

8383
#[salsa::invoke(Body::body_with_source_map_query)]
8484
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);

crates/ra_hir_ty/src/infer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_hash::FxHashMap;
2222

2323
use hir_def::{
2424
body::Body,
25-
data::{ConstData, FunctionData},
25+
data::{ConstData, FunctionData, StaticData},
2626
expr::{BindingAnnotation, ExprId, PatId},
2727
lang_item::LangItemTarget,
2828
path::{path, Path},
@@ -71,7 +71,7 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
7171
match def {
7272
DefWithBodyId::ConstId(c) => ctx.collect_const(&db.const_data(c)),
7373
DefWithBodyId::FunctionId(f) => ctx.collect_fn(&db.function_data(f)),
74-
DefWithBodyId::StaticId(s) => ctx.collect_const(&db.static_data(s)),
74+
DefWithBodyId::StaticId(s) => ctx.collect_static(&db.static_data(s)),
7575
}
7676

7777
ctx.infer_body();
@@ -485,6 +485,10 @@ impl<'a> InferenceContext<'a> {
485485
self.return_ty = self.make_ty(&data.type_ref);
486486
}
487487

488+
fn collect_static(&mut self, data: &StaticData) {
489+
self.return_ty = self.make_ty(&data.type_ref);
490+
}
491+
488492
fn collect_fn(&mut self, data: &FunctionData) {
489493
let body = Arc::clone(&self.body); // avoid borrow checker problem
490494
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
<span class="keyword">let</span> <span class="variable declaration">x</span> = <span class="numeric_literal">92</span>;
5757
<span class="variable mutable">vec</span>.<span class="unresolved_reference">push</span>(<span class="struct">Foo</span> { <span class="field">x</span>, <span class="field">y</span>: <span class="numeric_literal">1</span> });
5858
}
59-
<span class="keyword unsafe">unsafe</span> { <span class="variable mutable">vec</span>.<span class="unresolved_reference">set_len</span>(<span class="numeric_literal">0</span>); }
59+
<span class="keyword unsafe">unsafe</span> {
60+
<span class="variable mutable">vec</span>.<span class="unresolved_reference">set_len</span>(<span class="numeric_literal">0</span>);
61+
<span class="static mutable">STATIC_MUT</span> = <span class="numeric_literal">1</span>;
62+
}
6063

6164
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
6265
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,16 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
431431
hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Union,
432432
hir::ModuleDef::EnumVariant(_) => HighlightTag::EnumVariant,
433433
hir::ModuleDef::Const(_) => HighlightTag::Constant,
434-
hir::ModuleDef::Static(_) => HighlightTag::Static,
435434
hir::ModuleDef::Trait(_) => HighlightTag::Trait,
436435
hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias,
437436
hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
437+
hir::ModuleDef::Static(s) => {
438+
let mut h = Highlight::new(HighlightTag::Static);
439+
if s.is_mut(db) {
440+
h |= HighlightModifier::Mutable;
441+
}
442+
return h;
443+
}
438444
},
439445
Definition::SelfType(_) => HighlightTag::SelfType,
440446
Definition::TypeParam(_) => HighlightTag::TypeParam,

crates/ra_ide/src/syntax_highlighting/tests.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ struct Foo {
1717
pub y: i32,
1818
}
1919
20+
static mut STATIC_MUT: i32 = 0;
21+
2022
fn foo<'a, T>() -> T {
2123
foo::<'a, i32>()
2224
}
@@ -40,7 +42,10 @@ fn main() {
4042
let x = 92;
4143
vec.push(Foo { x, y: 1 });
4244
}
43-
unsafe { vec.set_len(0); }
45+
unsafe {
46+
vec.set_len(0);
47+
STATIC_MUT = 1;
48+
}
4449
4550
let mut x = 42;
4651
let y = &mut x;

0 commit comments

Comments
 (0)