Skip to content

Commit 6e7a97b

Browse files
committed
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer
2 parents 577f29d + 518f6d7 commit 6e7a97b

File tree

28 files changed

+768
-195
lines changed

28 files changed

+768
-195
lines changed

Cargo.lock

+27-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-12
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@ miniz_oxide.opt-level = 3
1919
incremental = true
2020
debug = 0 # Set this to 1 or 2 to get more useful backtraces in debugger.
2121

22-
# Ideally, we would use `build-override` here, but some crates are also
23-
# needed at run-time and we end up compiling them twice.
24-
[profile.release.package]
25-
chalk-derive.opt-level = 0
26-
proc-macro2.opt-level = 0
27-
quote.opt-level = 0
28-
salsa-macros.opt-level = 0
29-
serde_derive.opt-level = 0
30-
syn.opt-level = 0
31-
tracing-attributes.opt-level = 0
32-
xtask.opt-level = 0
33-
3422
[patch.'crates-io']
3523
# rowan = { path = "../rowan" }
3624

crates/assists/src/handlers/fix_visibility.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -324,23 +324,21 @@ pub struct Foo { pub bar: () }
324324

325325
#[test]
326326
fn fix_visibility_of_enum_variant_field() {
327-
check_assist(
327+
// Enum variants, as well as their fields, always get the enum's visibility. In fact, rustc
328+
// rejects any visibility specifiers on them, so this assist should never fire on them.
329+
check_assist_not_applicable(
328330
fix_visibility,
329331
r"mod foo { pub enum Foo { Bar { bar: () } } }
330332
fn main() { foo::Foo::Bar { <|>bar: () }; } ",
331-
r"mod foo { pub enum Foo { Bar { $0pub(crate) bar: () } } }
332-
fn main() { foo::Foo::Bar { bar: () }; } ",
333333
);
334-
check_assist(
334+
check_assist_not_applicable(
335335
fix_visibility,
336336
r"
337337
//- /lib.rs
338338
mod foo;
339339
fn main() { foo::Foo::Bar { <|>bar: () }; }
340340
//- /foo.rs
341341
pub enum Foo { Bar { bar: () } }
342-
",
343-
r"pub enum Foo { Bar { $0pub(crate) bar: () } }
344342
",
345343
);
346344
check_assist_not_applicable(

crates/hir/src/code_model.rs

+17
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ impl_from!(
186186
for ModuleDef
187187
);
188188

189+
impl From<VariantDef> for ModuleDef {
190+
fn from(var: VariantDef) -> Self {
191+
match var {
192+
VariantDef::Struct(t) => Adt::from(t).into(),
193+
VariantDef::Union(t) => Adt::from(t).into(),
194+
VariantDef::EnumVariant(t) => t.into(),
195+
}
196+
}
197+
}
198+
189199
impl ModuleDef {
190200
pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
191201
match self {
@@ -752,6 +762,13 @@ impl Function {
752762
pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
753763
hir_ty::diagnostics::validate_body(db, self.id.into(), sink)
754764
}
765+
766+
/// Whether this function declaration has a definition.
767+
///
768+
/// This is false in the case of required (not provided) trait methods.
769+
pub fn has_body(self, db: &dyn HirDatabase) -> bool {
770+
db.function_data(self.id).has_body
771+
}
755772
}
756773

757774
// Note: logically, this belongs to `hir_ty`, but we are not using it there yet.

crates/hir/src/semantics/source_to_def.rs

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ impl SourceToDefCtx<'_, '_> {
189189
let def = self.type_alias_to_def(container.with_value(it))?;
190190
def.into()
191191
},
192+
ast::Variant(it) => {
193+
let def = self.enum_variant_to_def(container.with_value(it))?;
194+
VariantId::from(def).into()
195+
},
192196
_ => continue,
193197
}
194198
};

crates/hir_def/src/adt.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tt::{Delimiter, DelimiterKind, Leaf, Subtree, TokenTree};
1414
use crate::{
1515
body::{CfgExpander, LowerCtx},
1616
db::DefDatabase,
17-
item_tree::{AttrOwner, Field, Fields, ItemTree, ModItem},
17+
item_tree::{AttrOwner, Field, Fields, ItemTree, ModItem, RawVisibilityId},
1818
src::HasChildSource,
1919
src::HasSource,
2020
trace::Trace,
@@ -91,7 +91,7 @@ impl StructData {
9191
let cfg_options = db.crate_graph()[loc.container.module(db).krate].cfg_options.clone();
9292

9393
let strukt = &item_tree[loc.id.value];
94-
let variant_data = lower_fields(&item_tree, &cfg_options, &strukt.fields);
94+
let variant_data = lower_fields(&item_tree, &cfg_options, &strukt.fields, None);
9595
Arc::new(StructData {
9696
name: strukt.name.clone(),
9797
variant_data: Arc::new(variant_data),
@@ -105,7 +105,7 @@ impl StructData {
105105
let cfg_options = db.crate_graph()[loc.container.module(db).krate].cfg_options.clone();
106106

107107
let union = &item_tree[loc.id.value];
108-
let variant_data = lower_fields(&item_tree, &cfg_options, &union.fields);
108+
let variant_data = lower_fields(&item_tree, &cfg_options, &union.fields, None);
109109

110110
Arc::new(StructData {
111111
name: union.name.clone(),
@@ -126,7 +126,8 @@ impl EnumData {
126126
for var_id in enum_.variants.clone() {
127127
if item_tree.attrs(var_id.into()).is_cfg_enabled(&cfg_options) {
128128
let var = &item_tree[var_id];
129-
let var_data = lower_fields(&item_tree, &cfg_options, &var.fields);
129+
let var_data =
130+
lower_fields(&item_tree, &cfg_options, &var.fields, Some(enum_.visibility));
130131

131132
variants.alloc(EnumVariantData {
132133
name: var.name.clone(),
@@ -296,13 +297,18 @@ fn lower_struct(
296297
}
297298
}
298299

299-
fn lower_fields(item_tree: &ItemTree, cfg_options: &CfgOptions, fields: &Fields) -> VariantData {
300+
fn lower_fields(
301+
item_tree: &ItemTree,
302+
cfg_options: &CfgOptions,
303+
fields: &Fields,
304+
override_visibility: Option<RawVisibilityId>,
305+
) -> VariantData {
300306
match fields {
301307
Fields::Record(flds) => {
302308
let mut arena = Arena::new();
303309
for field_id in flds.clone() {
304310
if item_tree.attrs(field_id.into()).is_cfg_enabled(cfg_options) {
305-
arena.alloc(lower_field(item_tree, &item_tree[field_id]));
311+
arena.alloc(lower_field(item_tree, &item_tree[field_id], override_visibility));
306312
}
307313
}
308314
VariantData::Record(arena)
@@ -311,7 +317,7 @@ fn lower_fields(item_tree: &ItemTree, cfg_options: &CfgOptions, fields: &Fields)
311317
let mut arena = Arena::new();
312318
for field_id in flds.clone() {
313319
if item_tree.attrs(field_id.into()).is_cfg_enabled(cfg_options) {
314-
arena.alloc(lower_field(item_tree, &item_tree[field_id]));
320+
arena.alloc(lower_field(item_tree, &item_tree[field_id], override_visibility));
315321
}
316322
}
317323
VariantData::Tuple(arena)
@@ -320,10 +326,14 @@ fn lower_fields(item_tree: &ItemTree, cfg_options: &CfgOptions, fields: &Fields)
320326
}
321327
}
322328

323-
fn lower_field(item_tree: &ItemTree, field: &Field) -> FieldData {
329+
fn lower_field(
330+
item_tree: &ItemTree,
331+
field: &Field,
332+
override_visibility: Option<RawVisibilityId>,
333+
) -> FieldData {
324334
FieldData {
325335
name: field.name.clone(),
326336
type_ref: field.type_ref.clone(),
327-
visibility: item_tree[field.visibility].clone(),
337+
visibility: item_tree[override_visibility.unwrap_or(field.visibility)].clone(),
328338
}
329339
}

crates/hir_def/src/data.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct FunctionData {
2525
/// True if the first param is `self`. This is relevant to decide whether this
2626
/// can be called as a method.
2727
pub has_self_param: bool,
28+
pub has_body: bool,
2829
pub is_unsafe: bool,
2930
pub is_varargs: bool,
3031
pub visibility: RawVisibility,
@@ -42,6 +43,7 @@ impl FunctionData {
4243
ret_type: func.ret_type.clone(),
4344
attrs: item_tree.attrs(ModItem::from(loc.id.value).into()).clone(),
4445
has_self_param: func.has_self_param,
46+
has_body: func.has_body,
4547
is_unsafe: func.is_unsafe,
4648
is_varargs: func.is_varargs,
4749
visibility: item_tree[func.visibility].clone(),

crates/hir_def/src/item_tree.rs

+1
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ pub struct Function {
505505
pub visibility: RawVisibilityId,
506506
pub generic_params: GenericParamsId,
507507
pub has_self_param: bool,
508+
pub has_body: bool,
508509
pub is_unsafe: bool,
509510
pub params: Box<[TypeRef]>,
510511
pub is_varargs: bool,

crates/hir_def/src/item_tree/lower.rs

+3
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,15 @@ impl Ctx {
330330
ret_type
331331
};
332332

333+
let has_body = func.body().is_some();
334+
333335
let ast_id = self.source_ast_id_map.ast_id(func);
334336
let mut res = Function {
335337
name,
336338
visibility,
337339
generic_params: GenericParamsId::EMPTY,
338340
has_self_param,
341+
has_body,
339342
is_unsafe: func.unsafe_token().is_some(),
340343
params: params.into_boxed_slice(),
341344
is_varargs,

0 commit comments

Comments
 (0)