Skip to content

Commit 9e13cd3

Browse files
committed
Resolve lang items in ast
1 parent e70cbef commit 9e13cd3

File tree

18 files changed

+362
-299
lines changed

18 files changed

+362
-299
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4064,6 +4064,7 @@ dependencies = [
40644064
"rustc_feature",
40654065
"rustc_hir",
40664066
"rustc_index",
4067+
"rustc_macros",
40674068
"rustc_metadata",
40684069
"rustc_middle",
40694070
"rustc_query_system",

compiler/rustc_ast/src/ast.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,7 @@ impl ItemKind {
28822882
}
28832883
}
28842884

2885-
pub fn descr(&self) -> &str {
2885+
pub fn descr(&self) -> &'static str {
28862886
match self {
28872887
ItemKind::ExternCrate(..) => "extern crate",
28882888
ItemKind::Use(..) => "`use` import",
@@ -2952,6 +2952,15 @@ impl AssocItemKind {
29522952
Self::MacCall(..) => Defaultness::Final,
29532953
}
29542954
}
2955+
2956+
pub fn generics(&self) -> Option<&Generics> {
2957+
match self {
2958+
Self::Const(..) => None,
2959+
Self::Fn(f) => Some(&f.generics),
2960+
Self::Type(t) => Some(&t.generics),
2961+
Self::MacCall(_) => None,
2962+
}
2963+
}
29552964
}
29562965

29572966
impl From<AssocItemKind> for ItemKind {

compiler/rustc_error_messages/locales/en-US/passes.ftl

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,6 @@ passes_deprecated_annotation_has_no_effect =
361361
this `#[deprecated]` annotation has no effect
362362
.suggestion = remove the unnecessary deprecation attribute
363363
364-
passes_unknown_external_lang_item =
365-
unknown external lang item: `{$lang_item}`
366-
367364
passes_missing_panic_handler =
368365
`#[panic_handler]` function required, but not found
369366
@@ -372,14 +369,6 @@ passes_missing_lang_item =
372369
.note = this can occur when a binary crate with `#![no_std]` is compiled for a target where `{$name}` is defined in the standard library
373370
.help = you may be able to compile for a target that doesn't need `{$name}`, specify a target with `--target` or in `.cargo/config`
374371
375-
passes_lang_item_on_incorrect_target =
376-
`{$name}` language item must be applied to a {$expected_target}
377-
.label = attribute should be applied to a {$expected_target}, not a {$actual_target}
378-
379-
passes_unknown_lang_item =
380-
definition of an unknown language item: `{$name}`
381-
.label = definition of unknown language item `{$name}`
382-
383372
passes_invalid_attr_at_crate_level =
384373
`{$name}` attribute cannot be used at crate level
385374
.suggestion = perhaps you meant to use an outer attribute
@@ -559,19 +548,6 @@ passes_duplicate_lang_item_crate_depends =
559548
.first_definition_path = first definition in `{$orig_crate_name}` loaded from {$orig_path}
560549
.second_definition_path = second definition in `{$crate_name}` loaded from {$path}
561550
562-
passes_incorrect_target =
563-
`{$name}` language item must be applied to a {$kind} with {$at_least ->
564-
[true] at least {$num}
565-
*[false] {$num}
566-
} generic {$num ->
567-
[one] argument
568-
*[other] arguments
569-
}
570-
.label = this {$kind} has {$actual_num} generic {$actual_num ->
571-
[one] argument
572-
*[other] arguments
573-
}
574-
575551
passes_useless_assignment =
576552
useless assignment of {$is_field_assign ->
577553
[true] field
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resolve_lang_item_on_incorrect_target =
2+
`{$name}` language item must be applied to a {$expected_target}
3+
.label = attribute should be applied to a {$expected_target}, not a {$actual_target}
4+
5+
resolve_unknown_external_lang_item =
6+
unknown external lang item: `{$lang_item}`
7+
8+
resolve_unknown_lang_item =
9+
definition of an unknown language item: `{$name}`
10+
.label = definition of unknown language item `{$name}`
11+
12+
resolve_incorrect_target =
13+
`{$name}` language item must be applied to a {$kind} with {$at_least ->
14+
[true] at least {$num}
15+
*[false] {$num}
16+
} generic {$num ->
17+
[one] argument
18+
*[other] arguments
19+
}
20+
.label = this {$kind} has {$actual_num} generic {$actual_num ->
21+
[one] argument
22+
*[other] arguments
23+
}

compiler/rustc_error_messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fluent_messages! {
6161
plugin_impl => "../locales/en-US/plugin_impl.ftl",
6262
privacy => "../locales/en-US/privacy.ftl",
6363
query_system => "../locales/en-US/query_system.ftl",
64+
resolve => "../locales/en-US/resolve.ftl",
6465
save_analysis => "../locales/en-US/save_analysis.ftl",
6566
session => "../locales/en-US/session.ftl",
6667
symbol_mangling => "../locales/en-US/symbol_mangling.ftl",

compiler/rustc_hir/src/target.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
//! conflicts between multiple such attributes attached to the same
55
//! item.
66
7+
use rustc_ast as ast;
8+
use rustc_ast::visit as ast_visit;
9+
710
use crate::hir;
811
use crate::{Item, ItemKind, TraitItem, TraitItemKind};
912

@@ -198,4 +201,41 @@ impl Target {
198201
Target::ExprField => "struct field",
199202
}
200203
}
204+
205+
pub fn from_ast_item(item: &ast::Item) -> Target {
206+
match item.kind {
207+
ast::ItemKind::ExternCrate(_) => Target::ExternCrate,
208+
ast::ItemKind::Use(_) => Target::Use,
209+
ast::ItemKind::Static(..) => Target::Static,
210+
ast::ItemKind::Const(..) => Target::Const,
211+
ast::ItemKind::Fn(_) => Target::Fn,
212+
ast::ItemKind::Mod(..) => Target::Mod,
213+
ast::ItemKind::ForeignMod(_) => Target::ForeignMod,
214+
ast::ItemKind::GlobalAsm(_) => Target::GlobalAsm,
215+
ast::ItemKind::TyAlias(_) => Target::TyAlias,
216+
ast::ItemKind::Enum(..) => Target::Enum,
217+
ast::ItemKind::Struct(..) => Target::Struct,
218+
ast::ItemKind::Union(..) => Target::Union,
219+
ast::ItemKind::Trait(_) => Target::Trait,
220+
ast::ItemKind::TraitAlias(..) => Target::TraitAlias,
221+
ast::ItemKind::Impl(_) => Target::Impl,
222+
ast::ItemKind::MacroDef(_) => Target::MacroDef,
223+
ast::ItemKind::MacCall(_) => panic!("unexpected MacCall"),
224+
}
225+
}
226+
227+
pub fn from_ast_assoc_item(kind: &ast::AssocItemKind, ctxt: ast_visit::AssocCtxt) -> Target {
228+
match kind {
229+
ast::AssocItemKind::Const(..) => Target::AssocConst,
230+
ast::AssocItemKind::Fn(f) => {
231+
let kind = match ctxt {
232+
ast_visit::AssocCtxt::Impl => MethodKind::Inherent,
233+
ast_visit::AssocCtxt::Trait => MethodKind::Trait { body: f.body.is_some() },
234+
};
235+
Target::Method(kind)
236+
}
237+
ast::AssocItemKind::Type(_) => Target::AssocTy,
238+
ast::AssocItemKind::MacCall(_) => panic!("unexpected MacCall"),
239+
}
240+
}
201241
}

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1515
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
1616
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
1717
use rustc_hir::diagnostic_items::DiagnosticItems;
18+
use rustc_hir::LangItem;
1819
use rustc_index::vec::{Idx, IndexVec};
1920
use rustc_middle::metadata::ModChild;
2021
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,10 +1908,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19081908

19091909
fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> {
19101910
empty_proc_macro!(self);
1911-
let lang_items = self.tcx.lang_items().iter();
1912-
self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| {
1913-
def_id.as_local().map(|id| (id.local_def_index, lang_item))
1914-
}))
1911+
let lang_items = self.tcx.resolutions(()).lang_items.iter();
1912+
self.lazy_array(lang_items.map(|&(def_id, lang_item)| (def_id.local_def_index, lang_item)))
19151913
}
19161914

19171915
fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_hir as hir;
3939
use rustc_hir::def::{CtorKind, CtorOf, DefKind, LifetimeRes, Res};
4040
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap};
4141
use rustc_hir::definitions::Definitions;
42-
use rustc_hir::Node;
42+
use rustc_hir::{LangItem, Node};
4343
use rustc_index::vec::IndexVec;
4444
use rustc_macros::HashStable;
4545
use rustc_query_system::ich::StableHashingContext;
@@ -178,6 +178,8 @@ pub struct ResolverGlobalCtxt {
178178
/// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`.
179179
pub confused_type_with_std_module: FxHashMap<Span, Span>,
180180
pub registered_tools: RegisteredTools,
181+
pub lang_items: Vec<(LocalDefId, LangItem)>,
182+
pub missing_lang_items: Vec<LangItem>,
181183
}
182184

183185
/// Resolutions that should only be used for lowering.

compiler/rustc_passes/src/errors.rs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55

66
use rustc_ast::Label;
77
use rustc_errors::{error_code, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
8-
use rustc_hir::{self as hir, ExprKind, Target};
8+
use rustc_hir::{self as hir, ExprKind};
99
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1010
use rustc_middle::ty::{MainDefinition, Ty};
1111
use rustc_span::{Span, Symbol, DUMMY_SP};
@@ -689,14 +689,6 @@ pub struct DeprecatedAnnotationHasNoEffect {
689689
pub span: Span,
690690
}
691691

692-
#[derive(Diagnostic)]
693-
#[diag(passes_unknown_external_lang_item, code = "E0264")]
694-
pub struct UnknownExternLangItem {
695-
#[primary_span]
696-
pub span: Span,
697-
pub lang_item: Symbol,
698-
}
699-
700692
#[derive(Diagnostic)]
701693
#[diag(passes_missing_panic_handler)]
702694
pub struct MissingPanicHandler;
@@ -709,26 +701,6 @@ pub struct MissingLangItem {
709701
pub name: Symbol,
710702
}
711703

712-
#[derive(Diagnostic)]
713-
#[diag(passes_lang_item_on_incorrect_target, code = "E0718")]
714-
pub struct LangItemOnIncorrectTarget {
715-
#[primary_span]
716-
#[label]
717-
pub span: Span,
718-
pub name: Symbol,
719-
pub expected_target: Target,
720-
pub actual_target: Target,
721-
}
722-
723-
#[derive(Diagnostic)]
724-
#[diag(passes_unknown_lang_item, code = "E0522")]
725-
pub struct UnknownLangItem {
726-
#[primary_span]
727-
#[label]
728-
pub span: Span,
729-
pub name: Symbol,
730-
}
731-
732704
pub struct InvalidAttrAtCrateLevel {
733705
pub span: Span,
734706
pub snippet: Option<String>,
@@ -1238,20 +1210,6 @@ impl IntoDiagnostic<'_> for DuplicateLangItem {
12381210
}
12391211
}
12401212

1241-
#[derive(Diagnostic)]
1242-
#[diag(passes_incorrect_target, code = "E0718")]
1243-
pub struct IncorrectTarget<'a> {
1244-
#[primary_span]
1245-
pub span: Span,
1246-
#[label]
1247-
pub generics_span: Span,
1248-
pub name: &'a str, // cannot be symbol because it renders e.g. `r#fn` instead of `fn`
1249-
pub kind: &'static str,
1250-
pub num: usize,
1251-
pub actual_num: usize,
1252-
pub at_least: bool,
1253-
}
1254-
12551213
#[derive(LintDiagnostic)]
12561214
#[diag(passes_useless_assignment)]
12571215
pub struct UselessAssignment<'a> {

0 commit comments

Comments
 (0)