Skip to content

Commit 6823b44

Browse files
hack for RustEmbed with updated test
1 parent 079556e commit 6823b44

File tree

5 files changed

+30
-43
lines changed

5 files changed

+30
-43
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
374374
module.underscore_disambiguator.update_unchecked(|d| d + 1);
375375
module.underscore_disambiguator.get()
376376
});
377+
let tcx = self.tcx();
378+
// "Same res different import" ambiguity hack for macros introduced in #145108.
379+
// See related discussion for more info:
380+
// https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Parallel.20Macro.20Expansion/near/542057918.
381+
if ns == MacroNS && binding.is_glob_import() {
382+
if let Some(def_id) = res.opt_def_id() {
383+
self.greatest_vis_map
384+
.entry(def_id)
385+
.and_modify(|vis| {
386+
if !vis.is_at_least(binding.vis, tcx) {
387+
*vis = binding.vis
388+
}
389+
})
390+
.or_insert(binding.vis);
391+
}
392+
}
377393
self.update_local_resolution(module, key, warn_ambiguity, |this, resolution| {
378394
if let Some(old_binding) = resolution.best_binding() {
379395
if res == Res::Err && old_binding.res() != Res::Err {
@@ -623,12 +639,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
623639
for (import, side_effect) in import_resolutions {
624640
let SideEffect { imported_module, bindings: side_effect_bindings } = side_effect;
625641
let parent = import.parent_scope.module;
626-
627642
match (&import.kind, side_effect_bindings) {
628643
(
629644
ImportKind::Single { target, bindings, .. },
630645
SideEffectBindings::Single { import_bindings },
631646
) => {
647+
debug!("{import_bindings:#?}");
632648
self.per_ns(|this, ns| {
633649
match import_bindings[ns] {
634650
Some(Some(binding)) => {
@@ -687,6 +703,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
687703
.resolution(import.parent_scope.module, key)
688704
.and_then(|r| r.binding())
689705
.is_some_and(|binding| binding.warn_ambiguity_recursive());
706+
debug!("defining binding from glob: {imported_binding:#?}");
690707
let _ = self.try_define_local(
691708
parent,
692709
key.ident.0,
@@ -1662,7 +1679,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16621679
next_binding = binding;
16631680
}
16641681

1665-
children.push(ModChild { ident: ident.0, res, vis: binding.vis, reexport_chain });
1682+
let vis = match res.opt_def_id() {
1683+
Some(def_id) => {
1684+
self.greatest_vis_map.get(&def_id).copied().unwrap_or(binding.vis)
1685+
}
1686+
None => binding.vis,
1687+
};
1688+
children.push(ModChild { ident: ident.0, res, vis, reexport_chain });
16661689
}
16671690
});
16681691

compiler/rustc_resolve/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,8 @@ pub struct Resolver<'ra, 'tcx> {
11691169
/// Crate-local macro expanded `macro_export` referred to by a module-relative path.
11701170
macro_expanded_macro_export_errors: BTreeSet<(Span, Span)> = BTreeSet::new(),
11711171

1172+
// Hack
1173+
greatest_vis_map: FxIndexMap<DefId, Visibility<DefId>>,
11721174
/// When a type is re-exported that has an inaccessible constructor because it has fields that
11731175
/// are inaccessible from the import's scope, we mark that as the type won't be able to be built
11741176
/// through the re-export. We use this information to extend the existing diagnostic.
@@ -1593,6 +1595,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15931595
glob_map: Default::default(),
15941596
used_imports: FxHashSet::default(),
15951597
maybe_unused_trait_imports: Default::default(),
1598+
greatest_vis_map: Default::default(),
15961599
inaccessible_ctor_reexport: Default::default(),
15971600

15981601
arenas,

tests/ui/imports/same-res-ambigious.fail.stderr

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/ui/imports/same-res-ambigious.pass.stderr

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
//@ check-pass
12
//@ edition: 2018
23
//@ revisions: fail pass
34
//@[pass] aux-crate: ambigious_extern=same-res-ambigious-extern.rs
45
//@[fail] aux-crate: ambigious_extern=same-res-ambigious-extern-fail.rs
56
// see https://github.com/rust-lang/rust/pull/147196
67

7-
#[derive(ambigious_extern::Embed)] //~ ERROR: derive macro `Embed` is private
8+
#[derive(ambigious_extern::Embed)]
89
struct Foo{}
910

1011
fn main(){}

0 commit comments

Comments
 (0)