Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2276448

Browse files
committed
Auto merge of #107164 - Mark-Simulacrum:beta-next, r=Mark-Simulacrum
[beta] backport * Don't wf-check non-local RPITs #107038 * Revert "Make PROC_MACRO_DERIVE_RESOLUTION_FALLBACK a hard error" #107133 * bump bootstrap 1.66.1 r? `@Mark-Simulacrum`
2 parents b7a6d74 + e9de5a6 commit 2276448

File tree

17 files changed

+584
-394
lines changed

17 files changed

+584
-394
lines changed

‎compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24182418
match path.res {
24192419
Res::Def(DefKind::OpaqueTy | DefKind::ImplTraitPlaceholder, did) => {
24202420
// Check for desugared `impl Trait`.
2421-
assert!(ty::is_impl_trait_defn(tcx, did).is_none());
2421+
assert!(tcx.is_type_alias_impl_trait(did));
24222422
let item_segment = path.segments.split_last().unwrap();
24232423
self.prohibit_generics(item_segment.1.iter(), |err| {
24242424
err.note("`impl Trait` types can't have type parameters");

‎compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn provide(providers: &mut Providers) {
8282
asm_target_features,
8383
collect_mod_item_types,
8484
should_inherit_track_caller,
85+
is_type_alias_impl_trait,
8586
..*providers
8687
};
8788
}
@@ -2250,3 +2251,13 @@ fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span:
22502251
}
22512252
}
22522253
}
2254+
2255+
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
2256+
match tcx.hir().get_if_local(def_id) {
2257+
Some(Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(opaque), .. })) => {
2258+
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias)
2259+
}
2260+
Some(_) => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
2261+
_ => bug!("tried getting opaque_ty_origin for non-local def-id {:?}", def_id),
2262+
}
2263+
}

‎compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,73 @@ 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+
19852052
declare_lint! {
19862053
/// The `macro_use_extern_crate` lint detects the use of the
19872054
/// [`macro_use` attribute].
@@ -3220,6 +3287,7 @@ declare_lint_pass! {
32203287
UNSTABLE_NAME_COLLISIONS,
32213288
IRREFUTABLE_LET_PATTERNS,
32223289
WHERE_CLAUSES_OBJECT_SAFETY,
3290+
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
32233291
MACRO_USE_EXTERN_CRATE,
32243292
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
32253293
ILL_FORMED_ATTRIBUTE_INPUT,

‎compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ provide! { tcx, def_id, other, cdata,
223223
generator_kind => { table }
224224
trait_def => { table }
225225
deduced_param_attrs => { table }
226+
is_type_alias_impl_trait => {
227+
debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
228+
cdata
229+
.root
230+
.tables
231+
.is_type_alias_impl_trait
232+
.get(cdata, def_id.index)
233+
.is_some()
234+
}
226235
collect_trait_impl_trait_tys => {
227236
Ok(cdata
228237
.root

‎compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15351535
hir::ItemKind::Mod(ref m) => {
15361536
return self.encode_info_for_mod(item.owner_id.def_id, m);
15371537
}
1538-
hir::ItemKind::OpaqueTy(..) => {
1538+
hir::ItemKind::OpaqueTy(ref opaque) => {
15391539
self.encode_explicit_item_bounds(def_id);
1540+
if matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias) {
1541+
self.tables.is_type_alias_impl_trait.set(def_id.index, ());
1542+
}
15401543
}
15411544
hir::ItemKind::Enum(..) => {
15421545
let adt_def = self.tcx.adt_def(def_id);

‎compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ define_tables! {
404404
proc_macro: Table<DefIndex, MacroKind>,
405405
module_reexports: Table<DefIndex, LazyArray<ModChild>>,
406406
deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
407+
// Slot is full when opaque is TAIT.
408+
is_type_alias_impl_trait: Table<DefIndex, ()>,
407409

408410
trait_impl_trait_tys: Table<DefIndex, LazyValue<FxHashMap<DefId, Ty<'static>>>>,
409411
}

‎compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ rustc_queries! {
175175
separate_provide_extern
176176
}
177177

178+
query is_type_alias_impl_trait(key: DefId) -> bool
179+
{
180+
desc { "determine whether the opaque is a type-alias impl trait" }
181+
separate_provide_extern
182+
}
183+
178184
query analysis(key: ()) -> Result<(), ErrorGuaranteed> {
179185
eval_always
180186
desc { "running analysis passes on this crate" }

‎compiler/rustc_middle/src/ty/parameterized.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ trivially_parameterized_over_tcx! {
5353
usize,
5454
(),
5555
u32,
56+
bool,
5657
std::string::String,
5758
crate::metadata::ModChild,
5859
crate::middle::codegen_fn_attrs::CodegenFnAttrs,

‎compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ impl<'a> Resolver<'a> {
10291029
let root_module = this.resolve_crate_root(root_ident);
10301030
this.add_module_candidates(root_module, &mut suggestions, filter_fn, None);
10311031
}
1032-
Scope::Module(module) => {
1032+
Scope::Module(module, _) => {
10331033
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
10341034
}
10351035
Scope::MacroUsePrelude => {

‎compiler/rustc_resolve/src/ident.rs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use rustc_ast as ast;
1+
use rustc_ast::{self as ast,NodeId};
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;
79
use rustc_span::def_id::LocalDefId;
810
use rustc_span::edition::Edition;
911
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
@@ -99,7 +101,7 @@ impl<'a> Resolver<'a> {
99101
};
100102
let mut scope = match ns {
101103
_ if is_absolute_path => Scope::CrateRoot,
102-
TypeNS | ValueNS => Scope::Module(module),
104+
TypeNS | ValueNS => Scope::Module(module,None),
103105
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
104106
};
105107
let mut ctxt = ctxt.normalize_to_macros_2_0();
@@ -163,7 +165,7 @@ impl<'a> Resolver<'a> {
163165
MacroRulesScope::Invocation(invoc_id) => {
164166
Scope::MacroRules(self.invocation_parent_scopes[&invoc_id].macro_rules)
165167
}
166-
MacroRulesScope::Empty => Scope::Module(module),
168+
MacroRulesScope::Empty => Scope::Module(module,None),
167169
},
168170
Scope::CrateRoot => match ns {
169171
TypeNS => {
@@ -172,10 +174,16 @@ impl<'a> Resolver<'a> {
172174
}
173175
ValueNS | MacroNS => break,
174176
},
175-
Scope::Module(module) => {
177+
Scope::Module(module, prev_lint_id) => {
176178
use_prelude = !module.no_implicit_prelude;
177-
match self.hygienic_lexical_parent(module, &mut ctxt) {
178-
Some(parent_module) => Scope::Module(parent_module),
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+
}
179187
None => {
180188
ctxt.adjust(ExpnId::root());
181189
match ns {
@@ -207,13 +215,45 @@ impl<'a> Resolver<'a> {
207215
&mut self,
208216
module: Module<'a>,
209217
ctxt: &mut SyntaxContext,
210-
) -> Option<Module<'a>> {
218+
derive_fallback_lint_id: Option<NodeId>,
219+
) -> Option<(Module<'a>, Option<NodeId>)> {
211220
if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
212-
return Some(self.expn_def_scope(ctxt.remove_mark()));
221+
return Some((self.expn_def_scope(ctxt.remove_mark()),None));
213222
}
214223

215224
if let ModuleKind::Block = module.kind {
216-
return Some(module.parent.unwrap().nearest_item_scope());
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+
}
217257
}
218258

219259
None
@@ -470,7 +510,7 @@ impl<'a> Resolver<'a> {
470510
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
471511
}
472512
}
473-
Scope::Module(module) => {
513+
Scope::Module(module, derive_fallback_lint_id) => {
474514
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
475515
let binding = this.resolve_ident_in_module_unadjusted_ext(
476516
ModuleOrUniformRoot::Module(module),
@@ -483,6 +523,21 @@ impl<'a> Resolver<'a> {
483523
);
484524
match binding {
485525
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+
}
486541
let misc_flags = if ptr::eq(module, this.graph_root) {
487542
Flags::MISC_SUGGEST_CRATE
488543
} else if module.is_normal() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /