Skip to content

Commit 12d32a0

Browse files
hack for RustEmbed with updated test
1 parent 88a88b9 commit 12d32a0

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
@@ -375,6 +375,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
375375
module.underscore_disambiguator.update_unchecked(|d| d + 1);
376376
module.underscore_disambiguator.get()
377377
});
378+
let tcx = self.tcx();
379+
// "Same res different import" ambiguity hack for macros introduced in #145108.
380+
// See related discussion for more info:
381+
// https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Parallel.20Macro.20Expansion/near/542057918.
382+
if ns == MacroNS && binding.is_glob_import() {
383+
if let Some(def_id) = res.opt_def_id() {
384+
self.greatest_vis_map
385+
.entry(def_id)
386+
.and_modify(|vis| {
387+
if !vis.is_at_least(binding.vis, tcx) {
388+
*vis = binding.vis
389+
}
390+
})
391+
.or_insert(binding.vis);
392+
}
393+
}
378394
self.update_local_resolution(module, key, warn_ambiguity, |this, resolution| {
379395
if let Some(old_binding) = resolution.best_binding() {
380396
if res == Res::Err && old_binding.res() != Res::Err {
@@ -624,12 +640,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
624640
for (import, side_effect) in import_resolutions {
625641
let SideEffect { imported_module, bindings: side_effect_bindings } = side_effect;
626642
let parent = import.parent_scope.module;
627-
628643
match (&import.kind, side_effect_bindings) {
629644
(
630645
ImportKind::Single { target, bindings, .. },
631646
SideEffectBindings::Single { import_bindings },
632647
) => {
648+
debug!("{import_bindings:#?}");
633649
self.per_ns(|this, ns| {
634650
match import_bindings[ns] {
635651
Some(Some(binding)) => {
@@ -688,6 +704,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
688704
.resolution(import.parent_scope.module, key)
689705
.and_then(|r| r.binding())
690706
.is_some_and(|binding| binding.warn_ambiguity_recursive());
707+
debug!("defining binding from glob: {imported_binding:#?}");
691708
let _ = self.try_define_local(
692709
parent,
693710
key.ident.0,
@@ -1656,7 +1673,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16561673
next_binding = binding;
16571674
}
16581675

1659-
children.push(ModChild { ident: ident.0, res, vis: binding.vis, reexport_chain });
1676+
let vis = match res.opt_def_id() {
1677+
Some(def_id) => {
1678+
self.greatest_vis_map.get(&def_id).copied().unwrap_or(binding.vis)
1679+
}
1680+
None => binding.vis,
1681+
};
1682+
children.push(ModChild { ident: ident.0, res, vis, reexport_chain });
16601683
}
16611684
});
16621685

compiler/rustc_resolve/src/lib.rs

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

1170+
// Hack
1171+
greatest_vis_map: FxIndexMap<DefId, Visibility<DefId>>,
11701172
/// When a type is re-exported that has an inaccessible constructor because it has fields that
11711173
/// are inaccessible from the import's scope, we mark that as the type won't be able to be built
11721174
/// through the re-export. We use this information to extend the existing diagnostic.
@@ -1600,6 +1602,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16001602
glob_map: Default::default(),
16011603
used_imports: FxHashSet::default(),
16021604
maybe_unused_trait_imports: Default::default(),
1605+
greatest_vis_map: Default::default(),
16031606
inaccessible_ctor_reexport: Default::default(),
16041607

16051608
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)