Skip to content

Commit 9f603fe

Browse files
authored
Rollup merge of #84022 - Aaron1011:remove-derive-res-fallback, r=petrochenkov
Make PROC_MACRO_DERIVE_RESOLUTION_FALLBACK a hard error r? `@ghost`
2 parents e70cbef + 7d82cad commit 9f603fe

File tree

6 files changed

+49
-238
lines changed

6 files changed

+49
-238
lines changed

compiler/rustc_lint_defs/src/builtin.rs

-68
Original file line numberDiff line numberDiff line change
@@ -1982,73 +1982,6 @@ declare_lint! {
19821982
};
19831983
}
19841984

1985-
declare_lint! {
1986-
/// The `proc_macro_derive_resolution_fallback` lint detects proc macro
1987-
/// derives using inaccessible names from parent modules.
1988-
///
1989-
/// ### Example
1990-
///
1991-
/// ```rust,ignore (proc-macro)
1992-
/// // foo.rs
1993-
/// #![crate_type = "proc-macro"]
1994-
///
1995-
/// extern crate proc_macro;
1996-
///
1997-
/// use proc_macro::*;
1998-
///
1999-
/// #[proc_macro_derive(Foo)]
2000-
/// pub fn foo1(a: TokenStream) -> TokenStream {
2001-
/// drop(a);
2002-
/// "mod __bar { static mut BAR: Option<Something> = None; }".parse().unwrap()
2003-
/// }
2004-
/// ```
2005-
///
2006-
/// ```rust,ignore (needs-dependency)
2007-
/// // bar.rs
2008-
/// #[macro_use]
2009-
/// extern crate foo;
2010-
///
2011-
/// struct Something;
2012-
///
2013-
/// #[derive(Foo)]
2014-
/// struct Another;
2015-
///
2016-
/// fn main() {}
2017-
/// ```
2018-
///
2019-
/// This will produce:
2020-
///
2021-
/// ```text
2022-
/// warning: cannot find type `Something` in this scope
2023-
/// --> src/main.rs:8:10
2024-
/// |
2025-
/// 8 | #[derive(Foo)]
2026-
/// | ^^^ names from parent modules are not accessible without an explicit import
2027-
/// |
2028-
/// = note: `#[warn(proc_macro_derive_resolution_fallback)]` on by default
2029-
/// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2030-
/// = note: for more information, see issue #50504 <https://github.com/rust-lang/rust/issues/50504>
2031-
/// ```
2032-
///
2033-
/// ### Explanation
2034-
///
2035-
/// If a proc-macro generates a module, the compiler unintentionally
2036-
/// allowed items in that module to refer to items in the crate root
2037-
/// without importing them. This is a [future-incompatible] lint to
2038-
/// transition this to a hard error in the future. See [issue #50504] for
2039-
/// more details.
2040-
///
2041-
/// [issue #50504]: https://github.com/rust-lang/rust/issues/50504
2042-
/// [future-incompatible]: ../index.md#future-incompatible-lints
2043-
pub PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
2044-
Deny,
2045-
"detects proc macro derives using inaccessible names from parent modules",
2046-
@future_incompatible = FutureIncompatibleInfo {
2047-
reference: "issue #83583 <https://github.com/rust-lang/rust/issues/83583>",
2048-
reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow,
2049-
};
2050-
}
2051-
20521985
declare_lint! {
20531986
/// The `macro_use_extern_crate` lint detects the use of the
20541987
/// [`macro_use` attribute].
@@ -3287,7 +3220,6 @@ declare_lint_pass! {
32873220
UNSTABLE_NAME_COLLISIONS,
32883221
IRREFUTABLE_LET_PATTERNS,
32893222
WHERE_CLAUSES_OBJECT_SAFETY,
3290-
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
32913223
MACRO_USE_EXTERN_CRATE,
32923224
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
32933225
ILL_FORMED_ATTRIBUTE_INPUT,

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ impl<'a> Resolver<'a> {
12021202
let root_module = this.resolve_crate_root(root_ident);
12031203
this.add_module_candidates(root_module, &mut suggestions, filter_fn, None);
12041204
}
1205-
Scope::Module(module, _) => {
1205+
Scope::Module(module) => {
12061206
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
12071207
}
12081208
Scope::MacroUsePrelude => {

compiler/rustc_resolve/src/ident.rs

+10-65
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use rustc_ast::{self as ast, NodeId};
1+
use rustc_ast as ast;
22
use rustc_feature::is_builtin_attr_name;
33
use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
44
use rustc_hir::PrimTy;
55
use rustc_middle::bug;
66
use rustc_middle::ty;
7-
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
8-
use rustc_session::lint::BuiltinLintDiagnostics;
97
use rustc_span::def_id::LocalDefId;
108
use rustc_span::edition::Edition;
119
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
@@ -101,7 +99,7 @@ impl<'a> Resolver<'a> {
10199
};
102100
let mut scope = match ns {
103101
_ if is_absolute_path => Scope::CrateRoot,
104-
TypeNS | ValueNS => Scope::Module(module, None),
102+
TypeNS | ValueNS => Scope::Module(module),
105103
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
106104
};
107105
let mut ctxt = ctxt.normalize_to_macros_2_0();
@@ -165,7 +163,7 @@ impl<'a> Resolver<'a> {
165163
MacroRulesScope::Invocation(invoc_id) => {
166164
Scope::MacroRules(self.invocation_parent_scopes[&invoc_id].macro_rules)
167165
}
168-
MacroRulesScope::Empty => Scope::Module(module, None),
166+
MacroRulesScope::Empty => Scope::Module(module),
169167
},
170168
Scope::CrateRoot => match ns {
171169
TypeNS => {
@@ -174,16 +172,10 @@ impl<'a> Resolver<'a> {
174172
}
175173
ValueNS | MacroNS => break,
176174
},
177-
Scope::Module(module, prev_lint_id) => {
175+
Scope::Module(module) => {
178176
use_prelude = !module.no_implicit_prelude;
179-
let derive_fallback_lint_id = match scope_set {
180-
ScopeSet::Late(.., lint_id) => lint_id,
181-
_ => None,
182-
};
183-
match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) {
184-
Some((parent_module, lint_id)) => {
185-
Scope::Module(parent_module, lint_id.or(prev_lint_id))
186-
}
177+
match self.hygienic_lexical_parent(module, &mut ctxt) {
178+
Some(parent_module) => Scope::Module(parent_module),
187179
None => {
188180
ctxt.adjust(ExpnId::root());
189181
match ns {
@@ -215,45 +207,13 @@ impl<'a> Resolver<'a> {
215207
&mut self,
216208
module: Module<'a>,
217209
ctxt: &mut SyntaxContext,
218-
derive_fallback_lint_id: Option<NodeId>,
219-
) -> Option<(Module<'a>, Option<NodeId>)> {
210+
) -> Option<Module<'a>> {
220211
if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
221-
return Some((self.expn_def_scope(ctxt.remove_mark()), None));
212+
return Some(self.expn_def_scope(ctxt.remove_mark()));
222213
}
223214

224215
if let ModuleKind::Block = module.kind {
225-
return Some((module.parent.unwrap().nearest_item_scope(), None));
226-
}
227-
228-
// We need to support the next case under a deprecation warning
229-
// ```
230-
// struct MyStruct;
231-
// ---- begin: this comes from a proc macro derive
232-
// mod implementation_details {
233-
// // Note that `MyStruct` is not in scope here.
234-
// impl SomeTrait for MyStruct { ... }
235-
// }
236-
// ---- end
237-
// ```
238-
// So we have to fall back to the module's parent during lexical resolution in this case.
239-
if derive_fallback_lint_id.is_some() {
240-
if let Some(parent) = module.parent {
241-
// Inner module is inside the macro, parent module is outside of the macro.
242-
if module.expansion != parent.expansion
243-
&& module.expansion.is_descendant_of(parent.expansion)
244-
{
245-
// The macro is a proc macro derive
246-
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
247-
let ext = self.get_macro_by_def_id(def_id).ext;
248-
if ext.builtin_name.is_none()
249-
&& ext.macro_kind() == MacroKind::Derive
250-
&& parent.expansion.outer_expn_is_descendant_of(*ctxt)
251-
{
252-
return Some((parent, derive_fallback_lint_id));
253-
}
254-
}
255-
}
256-
}
216+
return Some(module.parent.unwrap().nearest_item_scope());
257217
}
258218

259219
None
@@ -510,7 +470,7 @@ impl<'a> Resolver<'a> {
510470
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
511471
}
512472
}
513-
Scope::Module(module, derive_fallback_lint_id) => {
473+
Scope::Module(module) => {
514474
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
515475
let binding = this.resolve_ident_in_module_unadjusted_ext(
516476
ModuleOrUniformRoot::Module(module),
@@ -523,21 +483,6 @@ impl<'a> Resolver<'a> {
523483
);
524484
match binding {
525485
Ok(binding) => {
526-
if let Some(lint_id) = derive_fallback_lint_id {
527-
this.lint_buffer.buffer_lint_with_diagnostic(
528-
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
529-
lint_id,
530-
orig_ident.span,
531-
&format!(
532-
"cannot find {} `{}` in this scope",
533-
ns.descr(),
534-
ident
535-
),
536-
BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(
537-
orig_ident.span,
538-
),
539-
);
540-
}
541486
let misc_flags = if ptr::eq(module, this.graph_root) {
542487
Flags::MISC_SUGGEST_CRATE
543488
} else if module.is_normal() {

compiler/rustc_resolve/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ enum Scope<'a> {
104104
DeriveHelpersCompat,
105105
MacroRules(MacroRulesScopeRef<'a>),
106106
CrateRoot,
107-
// The node ID is for reporting the `PROC_MACRO_DERIVE_RESOLUTION_FALLBACK`
108-
// lint if it should be reported.
109-
Module(Module<'a>, Option<NodeId>),
107+
Module(Module<'a>),
110108
MacroUsePrelude,
111109
BuiltinAttrs,
112110
ExternPrelude,
@@ -1551,7 +1549,7 @@ impl<'a> Resolver<'a> {
15511549

15521550
self.visit_scopes(ScopeSet::All(TypeNS, false), parent_scope, ctxt, |this, scope, _, _| {
15531551
match scope {
1554-
Scope::Module(module, _) => {
1552+
Scope::Module(module) => {
15551553
this.traits_in_module(module, assoc_item, &mut found_traits);
15561554
}
15571555
Scope::StdLibPrelude => {

src/test/ui/proc-macro/generate-mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ struct S;
1515

1616
#[derive(generate_mod::CheckDerive)] //~ ERROR cannot find type `FromOutside` in this scope
1717
//~| ERROR cannot find type `OuterDerive` in this scope
18-
//~| WARN this was previously accepted
19-
//~| WARN this was previously accepted
2018
struct Z;
2119

2220
fn inner_block() {
2321
#[derive(generate_mod::CheckDerive)] //~ ERROR cannot find type `FromOutside` in this scope
2422
//~| ERROR cannot find type `OuterDerive` in this scope
25-
//~| WARN this was previously accepted
26-
//~| WARN this was previously accepted
2723
struct InnerZ;
2824
}
2925

30-
#[derive(generate_mod::CheckDeriveLint)] // OK, lint is suppressed
26+
#[derive(generate_mod::CheckDeriveLint)] //~ ERROR cannot find type `OuterDeriveLint` in this scope
27+
//~| ERROR cannot find type `FromOutside` in this scope
3128
struct W;
3229

3330
fn main() {}

0 commit comments

Comments
 (0)