Skip to content

Commit 700af65

Browse files
authored
Merge branch 'rust-analyzer:master' into rust-dependencies
2 parents 01b0fd8 + 719babd commit 700af65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2652
-1390
lines changed

crates/hir/src/lib.rs

+55-49
Original file line numberDiff line numberDiff line change
@@ -1388,9 +1388,13 @@ impl Function {
13881388
let loc = self.id.lookup(db.upcast());
13891389
let krate = loc.krate(db);
13901390
let def_map = db.crate_def_map(krate.into());
1391-
let name = &function_data.name;
1391+
let ast_id =
1392+
InFile::new(loc.id.file_id(), loc.id.item_tree(db.upcast())[loc.id.value].ast_id);
1393+
13921394
let mut exported_proc_macros = def_map.exported_proc_macros();
1393-
exported_proc_macros.find(|(_, mac_name)| mac_name == name).map(|(id, _)| MacroDef { id })
1395+
exported_proc_macros
1396+
.find(|&(id, _)| matches!(id.kind, MacroDefKind::ProcMacro(_, _, id) if id == ast_id))
1397+
.map(|(id, _)| MacroDef { id })
13941398
}
13951399

13961400
/// A textual representation of the HIR of this function for debugging purposes.
@@ -2436,7 +2440,7 @@ impl Impl {
24362440

24372441
#[derive(Clone, PartialEq, Eq, Debug)]
24382442
pub struct Type {
2439-
krate: CrateId,
2443+
krate: CrateId, // FIXME this is probably redundant with the TraitEnvironment
24402444
env: Arc<TraitEnvironment>,
24412445
ty: Ty,
24422446
}
@@ -2503,6 +2507,10 @@ impl Type {
25032507
matches!(self.ty.kind(Interner), TyKind::Ref(..))
25042508
}
25052509

2510+
pub fn is_slice(&self) -> bool {
2511+
matches!(self.ty.kind(Interner), TyKind::Slice(..))
2512+
}
2513+
25062514
pub fn is_usize(&self) -> bool {
25072515
matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
25082516
}
@@ -2525,36 +2533,25 @@ impl Type {
25252533
/// Checks that particular type `ty` implements `std::future::Future`.
25262534
/// This function is used in `.await` syntax completion.
25272535
pub fn impls_future(&self, db: &dyn HirDatabase) -> bool {
2528-
// No special case for the type of async block, since Chalk can figure it out.
2529-
2530-
let krate = self.krate;
2531-
2532-
let std_future_trait =
2533-
db.lang_item(krate, SmolStr::new_inline("future_trait")).and_then(|it| it.as_trait());
2536+
let std_future_trait = db
2537+
.lang_item(self.krate, SmolStr::new_inline("future_trait"))
2538+
.and_then(|it| it.as_trait());
25342539
let std_future_trait = match std_future_trait {
25352540
Some(it) => it,
25362541
None => return false,
25372542
};
25382543

25392544
let canonical_ty =
25402545
Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) };
2541-
method_resolution::implements_trait(
2542-
&canonical_ty,
2543-
db,
2544-
self.env.clone(),
2545-
krate,
2546-
std_future_trait,
2547-
)
2546+
method_resolution::implements_trait(&canonical_ty, db, self.env.clone(), std_future_trait)
25482547
}
25492548

25502549
/// Checks that particular type `ty` implements `std::ops::FnOnce`.
25512550
///
25522551
/// This function can be used to check if a particular type is callable, since FnOnce is a
25532552
/// supertrait of Fn and FnMut, so all callable types implements at least FnOnce.
25542553
pub fn impls_fnonce(&self, db: &dyn HirDatabase) -> bool {
2555-
let krate = self.krate;
2556-
2557-
let fnonce_trait = match FnTrait::FnOnce.get_id(db, krate) {
2554+
let fnonce_trait = match FnTrait::FnOnce.get_id(db, self.krate) {
25582555
Some(it) => it,
25592556
None => return false,
25602557
};
@@ -2565,7 +2562,6 @@ impl Type {
25652562
&canonical_ty,
25662563
db,
25672564
self.env.clone(),
2568-
krate,
25692565
fnonce_trait,
25702566
)
25712567
}
@@ -2736,9 +2732,8 @@ impl Type {
27362732
pub fn autoderef_<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Ty> + 'a {
27372733
// There should be no inference vars in types passed here
27382734
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
2739-
let environment = self.env.env.clone();
2740-
let ty = InEnvironment { goal: canonical, environment };
2741-
autoderef(db, Some(self.krate), ty).map(|canonical| canonical.value)
2735+
let environment = self.env.clone();
2736+
autoderef(db, environment, canonical).map(|canonical| canonical.value)
27422737
}
27432738

27442739
// This would be nicer if it just returned an iterator, but that runs into
@@ -2793,24 +2788,26 @@ impl Type {
27932788
pub fn iterate_method_candidates<T>(
27942789
&self,
27952790
db: &dyn HirDatabase,
2796-
krate: Crate,
2791+
scope: &SemanticsScope,
2792+
// FIXME this can be retrieved from `scope`, except autoimport uses this
2793+
// to specify a different set, so the method needs to be split
27972794
traits_in_scope: &FxHashSet<TraitId>,
27982795
with_local_impls: Option<Module>,
27992796
name: Option<&Name>,
2800-
mut callback: impl FnMut(Type, Function) -> Option<T>,
2797+
mut callback: impl FnMut(Function) -> Option<T>,
28012798
) -> Option<T> {
28022799
let _p = profile::span("iterate_method_candidates");
28032800
let mut slot = None;
28042801

28052802
self.iterate_method_candidates_dyn(
28062803
db,
2807-
krate,
2804+
scope,
28082805
traits_in_scope,
28092806
with_local_impls,
28102807
name,
2811-
&mut |ty, assoc_item_id| {
2808+
&mut |assoc_item_id| {
28122809
if let AssocItemId::FunctionId(func) = assoc_item_id {
2813-
if let Some(res) = callback(self.derived(ty.clone()), func.into()) {
2810+
if let Some(res) = callback(func.into()) {
28142811
slot = Some(res);
28152812
return ControlFlow::Break(());
28162813
}
@@ -2824,50 +2821,55 @@ impl Type {
28242821
fn iterate_method_candidates_dyn(
28252822
&self,
28262823
db: &dyn HirDatabase,
2827-
krate: Crate,
2824+
scope: &SemanticsScope,
28282825
traits_in_scope: &FxHashSet<TraitId>,
28292826
with_local_impls: Option<Module>,
28302827
name: Option<&Name>,
2831-
callback: &mut dyn FnMut(&Ty, AssocItemId) -> ControlFlow<()>,
2828+
callback: &mut dyn FnMut(AssocItemId) -> ControlFlow<()>,
28322829
) {
28332830
// There should be no inference vars in types passed here
28342831
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
28352832

2836-
let env = self.env.clone();
2837-
let krate = krate.id;
2833+
let krate = match scope.krate() {
2834+
Some(k) => k,
2835+
None => return,
2836+
};
2837+
let environment = scope.resolver().generic_def().map_or_else(
2838+
|| Arc::new(TraitEnvironment::empty(krate.id)),
2839+
|d| db.trait_environment(d),
2840+
);
28382841

28392842
method_resolution::iterate_method_candidates_dyn(
28402843
&canonical,
28412844
db,
2842-
env,
2843-
krate,
2845+
environment,
28442846
traits_in_scope,
28452847
with_local_impls.and_then(|b| b.id.containing_block()).into(),
28462848
name,
28472849
method_resolution::LookupMode::MethodCall,
2848-
&mut |ty, id| callback(&ty.value, id),
2850+
&mut |_adj, id| callback(id),
28492851
);
28502852
}
28512853

28522854
pub fn iterate_path_candidates<T>(
28532855
&self,
28542856
db: &dyn HirDatabase,
2855-
krate: Crate,
2857+
scope: &SemanticsScope,
28562858
traits_in_scope: &FxHashSet<TraitId>,
28572859
with_local_impls: Option<Module>,
28582860
name: Option<&Name>,
2859-
mut callback: impl FnMut(Type, AssocItem) -> Option<T>,
2861+
mut callback: impl FnMut(AssocItem) -> Option<T>,
28602862
) -> Option<T> {
28612863
let _p = profile::span("iterate_path_candidates");
28622864
let mut slot = None;
28632865
self.iterate_path_candidates_dyn(
28642866
db,
2865-
krate,
2867+
scope,
28662868
traits_in_scope,
28672869
with_local_impls,
28682870
name,
2869-
&mut |ty, assoc_item_id| {
2870-
if let Some(res) = callback(self.derived(ty.clone()), assoc_item_id.into()) {
2871+
&mut |assoc_item_id| {
2872+
if let Some(res) = callback(assoc_item_id.into()) {
28712873
slot = Some(res);
28722874
return ControlFlow::Break(());
28732875
}
@@ -2880,27 +2882,31 @@ impl Type {
28802882
fn iterate_path_candidates_dyn(
28812883
&self,
28822884
db: &dyn HirDatabase,
2883-
krate: Crate,
2885+
scope: &SemanticsScope,
28842886
traits_in_scope: &FxHashSet<TraitId>,
28852887
with_local_impls: Option<Module>,
28862888
name: Option<&Name>,
2887-
callback: &mut dyn FnMut(&Ty, AssocItemId) -> ControlFlow<()>,
2889+
callback: &mut dyn FnMut(AssocItemId) -> ControlFlow<()>,
28882890
) {
28892891
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
28902892

2891-
let env = self.env.clone();
2892-
let krate = krate.id;
2893+
let krate = match scope.krate() {
2894+
Some(k) => k,
2895+
None => return,
2896+
};
2897+
let environment = scope.resolver().generic_def().map_or_else(
2898+
|| Arc::new(TraitEnvironment::empty(krate.id)),
2899+
|d| db.trait_environment(d),
2900+
);
28932901

2894-
method_resolution::iterate_method_candidates_dyn(
2902+
method_resolution::iterate_path_candidates(
28952903
&canonical,
28962904
db,
2897-
env,
2898-
krate,
2905+
environment,
28992906
traits_in_scope,
29002907
with_local_impls.and_then(|b| b.id.containing_block()).into(),
29012908
name,
2902-
method_resolution::LookupMode::Path,
2903-
&mut |ty, id| callback(&ty.value, id),
2909+
&mut |id| callback(id),
29042910
);
29052911
}
29062912

crates/hir/src/semantics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,10 @@ impl<'a> SemanticsScope<'a> {
12301230
Some(Crate { id: self.resolver.krate()? })
12311231
}
12321232

1233+
pub(crate) fn resolver(&self) -> &Resolver {
1234+
&self.resolver
1235+
}
1236+
12331237
/// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type
12341238
pub fn visible_traits(&self) -> FxHashSet<TraitId> {
12351239
let resolver = &self.resolver;

crates/hir_def/src/macro_expansion_tests/builtin_fn_macro.rs

+18
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,24 @@ fn main() { "foor0bar\nfalse"; }
313313
);
314314
}
315315

316+
#[test]
317+
fn test_concat_bytes_expand() {
318+
check(
319+
r##"
320+
#[rustc_builtin_macro]
321+
macro_rules! concat_bytes {}
322+
323+
fn main() { concat_bytes!(b'A', b"BC", [68, b'E', 70]); }
324+
"##,
325+
expect![[r##"
326+
#[rustc_builtin_macro]
327+
macro_rules! concat_bytes {}
328+
329+
fn main() { [b'A', 66, 67, 68, b'E', 70]; }
330+
"##]],
331+
);
332+
}
333+
316334
#[test]
317335
fn test_concat_with_captured_expr() {
318336
check(

0 commit comments

Comments
 (0)