Skip to content

Commit 8094e05

Browse files
committed
Auto merge of rust-lang#15202 - Veykril:builtin-derive-diags, r=Veykril
internal: Don't diagnose builtin derives
2 parents e175595 + 321e570 commit 8094e05

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

crates/hir-def/src/macro_expansion_tests/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
239239

240240
for impl_id in def_map[local_id].scope.impls() {
241241
let src = impl_id.lookup(&db).source(&db);
242-
if src.file_id.is_builtin_derive(&db).is_some() {
242+
if src.file_id.is_builtin_derive(&db) {
243243
let pp = pretty_print_macro_expansion(src.value.syntax().clone(), None);
244244
format_to!(expanded_text, "\n{}", pp)
245245
}

crates/hir-expand/src/lib.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ impl HirFileId {
319319
})
320320
}
321321

322-
/// Indicate it is macro file generated for builtin derive
323-
pub fn is_builtin_derive(&self, db: &dyn db::ExpandDatabase) -> Option<InFile<ast::Attr>> {
322+
pub fn as_builtin_derive_attr_node(
323+
&self,
324+
db: &dyn db::ExpandDatabase,
325+
) -> Option<InFile<ast::Attr>> {
324326
let macro_file = self.macro_file()?;
325327
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
326328
let attr = match loc.def.kind {
@@ -333,8 +335,22 @@ impl HirFileId {
333335
pub fn is_custom_derive(&self, db: &dyn db::ExpandDatabase) -> bool {
334336
match self.macro_file() {
335337
Some(macro_file) => {
336-
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
337-
matches!(loc.def.kind, MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _))
338+
matches!(
339+
db.lookup_intern_macro_call(macro_file.macro_call_id).def.kind,
340+
MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _)
341+
)
342+
}
343+
None => false,
344+
}
345+
}
346+
347+
pub fn is_builtin_derive(&self, db: &dyn db::ExpandDatabase) -> bool {
348+
match self.macro_file() {
349+
Some(macro_file) => {
350+
matches!(
351+
db.lookup_intern_macro_call(macro_file.macro_call_id).def.kind,
352+
MacroDefKind::BuiltInDerive(..)
353+
)
338354
}
339355
None => false,
340356
}
@@ -344,8 +360,7 @@ impl HirFileId {
344360
pub fn is_include_macro(&self, db: &dyn db::ExpandDatabase) -> bool {
345361
match self.macro_file() {
346362
Some(macro_file) => {
347-
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
348-
loc.def.is_include()
363+
db.lookup_intern_macro_call(macro_file.macro_call_id).def.is_include()
349364
}
350365
_ => false,
351366
}

crates/hir/src/lib.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -614,15 +614,21 @@ impl Module {
614614
let inherent_impls = db.inherent_impls_in_crate(self.id.krate());
615615

616616
for impl_def in self.impl_defs(db) {
617+
let loc = impl_def.id.lookup(db.upcast());
618+
let tree = loc.id.item_tree(db.upcast());
619+
let node = &tree[loc.id.value];
620+
let file_id = loc.id.file_id();
621+
if file_id.is_builtin_derive(db.upcast()) {
622+
// these expansion come from us, diagnosing them is a waste of resources
623+
// FIXME: Once we diagnose the inputs to builtin derives, we should at least extract those diagnostics somehow
624+
continue;
625+
}
626+
617627
for diag in db.impl_data_with_diagnostics(impl_def.id).1.iter() {
618628
emit_def_diagnostic(db, acc, diag);
619629
}
620630

621631
if inherent_impls.invalid_impls().contains(&impl_def.id) {
622-
let loc = impl_def.id.lookup(db.upcast());
623-
let tree = loc.id.item_tree(db.upcast());
624-
let node = &tree[loc.id.value];
625-
let file_id = loc.id.file_id();
626632
let ast_id_map = db.ast_id_map(file_id);
627633

628634
acc.push(IncoherentImpl { impl_: ast_id_map.get(node.ast_id()), file_id }.into())
@@ -3278,9 +3284,9 @@ impl Impl {
32783284
self.id.lookup(db.upcast()).container.into()
32793285
}
32803286

3281-
pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> {
3287+
pub fn as_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> {
32823288
let src = self.source(db)?;
3283-
src.file_id.is_builtin_derive(db.upcast())
3289+
src.file_id.as_builtin_derive_attr_node(db.upcast())
32843290
}
32853291
}
32863292

crates/ide/src/navigation_target.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,11 @@ impl ToNav for hir::Module {
357357
impl TryToNav for hir::Impl {
358358
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
359359
let InFile { file_id, value } = self.source(db)?;
360-
let derive_attr = self.is_builtin_derive(db);
360+
let derive_attr = self.as_builtin_derive(db);
361361

362-
let focus = if derive_attr.is_some() { None } else { value.self_ty() };
363-
364-
let syntax = match &derive_attr {
365-
Some(attr) => attr.value.syntax(),
366-
None => value.syntax(),
362+
let (focus, syntax) = match &derive_attr {
363+
Some(attr) => (None, attr.value.syntax()),
364+
None => (value.self_ty(), value.syntax()),
367365
};
368366

369367
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, syntax, focus);

0 commit comments

Comments
 (0)