Skip to content

Commit

Permalink
Merge #7644 #7648
Browse files Browse the repository at this point in the history
7644: Primitive completion r=jonas-schievink a=jonas-schievink

Fixes #7642

7648: fix nightly warning `legacy_derive_helpers` r=jonas-schievink a=peddermaster2

With a recent nightly (e.g. 2021-02-10) a warning comes up. This PR reorders the attributes to fix the warning.

See rust-lang/rust#79202

Co-authored-by: Jonas Schievink <[email protected]>
Co-authored-by: Peter Wischer <[email protected]>
  • Loading branch information
3 people authored Feb 12, 2021
3 parents e0fc2af + e938d76 + 23bf030 commit c155792
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 30 deletions.
34 changes: 33 additions & 1 deletion crates/completion/src/completions/qualified_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
}
}
PathResolution::Def(def @ hir::ModuleDef::Adt(_))
| PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) => {
| PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
| PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
for variant in e.variants(ctx.db) {
acc.add_enum_variant(ctx, variant, None);
Expand All @@ -59,6 +60,13 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
let ty = match def {
hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
hir::ModuleDef::BuiltinType(builtin) => {
let module = match ctx.scope.module() {
Some(it) => it,
None => return,
};
builtin.ty(ctx.db, module)
}
_ => unreachable!(),
};

Expand Down Expand Up @@ -780,4 +788,28 @@ impl Foo {
"#]],
);
}

#[test]
fn completes_primitive_assoc_const() {
check(
r#"
//- /lib.rs crate:lib deps:core
fn f() {
u8::$0
}
//- /core.rs crate:core
#[lang = "u8"]
impl u8 {
pub const MAX: Self = 255;
pub fn func(self) {}
}
"#,
expect![[r#"
ct MAX pub const MAX: Self = 255;
me func(…) -> ()
"#]],
);
}
}
20 changes: 18 additions & 2 deletions crates/hir/src/code_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use base_db::{CrateDisplayName, CrateId, Edition, FileId};
use either::Either;
use hir_def::{
adt::{ReprKind, StructKind, VariantData},
builtin_type::BuiltinType,
expr::{BindingAnnotation, LabelId, Pat, PatId},
import_map,
item_tree::ItemTreeNode,
Expand Down Expand Up @@ -245,7 +244,7 @@ impl ModuleDef {
ModuleDef::Const(it) => it.name(db),
ModuleDef::Static(it) => it.name(db),

ModuleDef::BuiltinType(it) => Some(it.as_name()),
ModuleDef::BuiltinType(it) => Some(it.name()),
}
}

Expand Down Expand Up @@ -994,6 +993,23 @@ impl HasVisibility for TypeAlias {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BuiltinType {
pub(crate) inner: hir_def::builtin_type::BuiltinType,
}

impl BuiltinType {
pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type {
let resolver = module.id.resolver(db.upcast());
Type::new_with_resolver(db, &resolver, Ty::builtin(self.inner))
.expect("crate not present in resolver")
}

pub fn name(self) -> Name {
self.inner.as_name()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDef {
pub(crate) id: MacroDefId,
Expand Down
21 changes: 17 additions & 4 deletions crates/hir/src/from_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use hir_def::{
};

use crate::{
code_model::GenericParam, Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local,
MacroDef, ModuleDef, Variant, VariantDef,
code_model::{BuiltinType, GenericParam},
Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant,
VariantDef,
};

macro_rules! from_id {
Expand Down Expand Up @@ -111,7 +112,7 @@ impl From<ModuleDefId> for ModuleDef {
ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()),
ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()),
ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it),
ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it.into()),
}
}
}
Expand All @@ -127,7 +128,7 @@ impl From<ModuleDef> for ModuleDefId {
ModuleDef::Static(it) => ModuleDefId::StaticId(it.into()),
ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()),
ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()),
ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it),
ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it.into()),
}
}
}
Expand Down Expand Up @@ -274,3 +275,15 @@ impl From<ModuleDef> for ItemInNs {
}
}
}

impl From<hir_def::builtin_type::BuiltinType> for BuiltinType {
fn from(inner: hir_def::builtin_type::BuiltinType) -> Self {
Self { inner }
}
}

impl From<BuiltinType> for hir_def::builtin_type::BuiltinType {
fn from(it: BuiltinType) -> Self {
it.inner
}
}
2 changes: 1 addition & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl PathResolution {
match self {
PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())),
PathResolution::Def(ModuleDef::BuiltinType(builtin)) => {
Some(TypeNs::BuiltinType(*builtin))
Some(TypeNs::BuiltinType((*builtin).into()))
}
PathResolution::Def(ModuleDef::Const(_))
| PathResolution::Def(ModuleDef::Variant(_))
Expand Down
9 changes: 5 additions & 4 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ use syntax::{
};

use crate::{
db::HirDatabase, semantics::PathResolution, Adt, Const, Field, Function, Local, MacroDef,
ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Variant,
code_model::BuiltinType, db::HirDatabase, semantics::PathResolution, Adt, Const, Field,
Function, Local, MacroDef, ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam,
Variant,
};
use base_db::CrateId;

Expand Down Expand Up @@ -479,7 +480,7 @@ fn resolve_hir_path_(
}
TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
})
};
Expand Down Expand Up @@ -555,7 +556,7 @@ fn resolve_hir_path_qualifier(
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
})
}
1 change: 1 addition & 0 deletions crates/hir_def/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ impl Resolver {
}

pub fn krate(&self) -> Option<CrateId> {
// FIXME: can this ever be `None`?
self.module_scope().map(|t| t.0.krate())
}

Expand Down
10 changes: 10 additions & 0 deletions crates/hir_ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{iter, mem, ops::Deref, sync::Arc};

use base_db::{salsa, CrateId};
use hir_def::{
builtin_type::BuiltinType,
expr::ExprId,
type_ref::{Mutability, Rawness},
AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
Expand Down Expand Up @@ -738,6 +739,15 @@ impl Ty {
Substs(sig.params_and_return),
)
}
pub fn builtin(builtin: BuiltinType) -> Self {
Ty::simple(match builtin {
BuiltinType::Char => TypeCtor::Char,
BuiltinType::Bool => TypeCtor::Bool,
BuiltinType::Str => TypeCtor::Str,
BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()),
BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()),
})
}

pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
match self {
Expand Down
14 changes: 1 addition & 13 deletions crates/hir_ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use test_utils::mark;

use crate::{
db::HirDatabase,
primitive::{FloatTy, IntTy},
utils::{
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
make_mut_slice, variant_data,
Expand Down Expand Up @@ -1051,17 +1050,6 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
Binders::new(0, Ty::from_hir(&ctx, &data.type_ref))
}

/// Build the declared type of a static.
fn type_for_builtin(def: BuiltinType) -> Ty {
Ty::simple(match def {
BuiltinType::Char => TypeCtor::Char,
BuiltinType::Bool => TypeCtor::Bool,
BuiltinType::Str => TypeCtor::Str,
BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()),
BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()),
})
}

fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig {
let struct_data = db.struct_data(def);
let fields = struct_data.variant_data.fields();
Expand Down Expand Up @@ -1186,7 +1174,7 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V
/// namespace.
pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
match def {
TyDefId::BuiltinType(it) => Binders::new(0, type_for_builtin(it)),
TyDefId::BuiltinType(it) => Binders::new(0, Ty::builtin(it)),
TyDefId::AdtId(it) => type_for_adt(db, it),
TyDefId::TypeAliasId(it) => type_for_type_alias(db, it),
}
Expand Down
5 changes: 2 additions & 3 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use url::Url;

use hir::{
db::{DefDatabase, HirDatabase},
Adt, AsAssocItem, AsName, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs,
ModuleDef,
Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, ModuleDef,
};
use ide_db::{
defs::{Definition, NameClass, NameRefClass},
Expand Down Expand Up @@ -429,7 +428,7 @@ fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<S
ModuleDef::Module(_) => "index.html".to_string(),
ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()),
ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.name()),
ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
ModuleDef::Variant(ev) => {
format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db))
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
ModuleDef::Static(it) => from_def_source(db, it, mod_path),
ModuleDef::Trait(it) => from_def_source(db, it, mod_path),
ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)),
ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it.name())),
},
Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
Definition::SelfType(impl_def) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/lsp_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ pub struct SsrParams {

pub enum StatusNotification {}

#[serde(rename_all = "camelCase")]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Status {
Loading,
ReadyPartial,
Expand Down

0 comments on commit c155792

Please sign in to comment.