Skip to content

Commit 8bf776d

Browse files
committed
Auto merge of #64478 - Centril:rollup-bnt51w1, r=Centril
Rollup of 5 pull requests Successful merges: - #64457 (def_collector: Do not ICE on attributes on unnamed fields) - #64463 (resolve: Tweak some expected/found wording) - #64471 (Warn on no_start, crate_id attribute use) - #64473 (Use try_fold instead of manually carrying an accumulator) - #64475 (simplify the initialization) Failed merges: r? @ghost
2 parents 572d3d9 + 88bd564 commit 8bf776d

File tree

14 files changed

+88
-47
lines changed

14 files changed

+88
-47
lines changed

src/librustc/hir/map/def_collector.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<'a> DefCollector<'a> {
3131
self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
3232
}
3333

34-
pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
34+
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
3535
let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
3636
f(self);
3737
self.parent_def = orig_parent_def;
@@ -74,6 +74,22 @@ impl<'a> DefCollector<'a> {
7474
})
7575
}
7676

77+
fn collect_field(&mut self, field: &'a StructField, index: Option<usize>) {
78+
if field.is_placeholder {
79+
self.visit_macro_invoc(field.id);
80+
} else {
81+
let name = field.ident.map(|ident| ident.name)
82+
.or_else(|| index.map(sym::integer))
83+
.unwrap_or_else(|| {
84+
let node_id = NodeId::placeholder_from_expn_id(self.expansion);
85+
sym::integer(self.definitions.placeholder_field_indices[&node_id])
86+
})
87+
.as_interned_str();
88+
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
89+
self.with_parent(def, |this| visit::walk_struct_field(this, field));
90+
}
91+
}
92+
7793
pub fn visit_macro_invoc(&mut self, id: NodeId) {
7894
self.definitions.set_invocation_parent(id.placeholder_to_expn_id(), self.parent_def);
7995
}
@@ -170,17 +186,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
170186
}
171187

172188
fn visit_variant_data(&mut self, data: &'a VariantData) {
189+
// The assumption here is that non-`cfg` macro expansion cannot change field indices.
190+
// It currently holds because only inert attributes are accepted on fields,
191+
// and every such attribute expands into a single field after it's resolved.
173192
for (index, field) in data.fields().iter().enumerate() {
174-
if field.is_placeholder {
175-
self.visit_macro_invoc(field.id);
176-
continue;
193+
self.collect_field(field, Some(index));
194+
if field.is_placeholder && field.ident.is_none() {
195+
self.definitions.placeholder_field_indices.insert(field.id, index);
177196
}
178-
let name = field.ident.map(|ident| ident.name)
179-
.unwrap_or_else(|| sym::integer(index));
180-
let def = self.create_def(field.id,
181-
DefPathData::ValueNs(name.as_interned_str()),
182-
field.span);
183-
self.with_parent(def, |this| visit::walk_struct_field(this, field));
184197
}
185198
}
186199

@@ -338,16 +351,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
338351
}
339352
}
340353

341-
fn visit_struct_field(&mut self, sf: &'a StructField) {
342-
if sf.is_placeholder {
343-
self.visit_macro_invoc(sf.id)
344-
} else {
345-
let name = sf.ident.map(|ident| ident.name)
346-
.unwrap_or_else(|| panic!("don't know the field number in this context"));
347-
let def = self.create_def(sf.id,
348-
DefPathData::ValueNs(name.as_interned_str()),
349-
sf.span);
350-
self.with_parent(def, |this| visit::walk_struct_field(this, sf));
351-
}
354+
// This method is called only when we are visiting an individual field
355+
// after expanding an attribute on it.
356+
fn visit_struct_field(&mut self, field: &'a StructField) {
357+
self.collect_field(field, None);
352358
}
353359
}

src/librustc/hir/map/definitions.rs

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ pub struct Definitions {
104104
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
105105
/// we know what parent node that fragment should be attached to thanks to this table.
106106
invocation_parents: FxHashMap<ExpnId, DefIndex>,
107+
/// Indices of unnamed struct or variant fields with unresolved attributes.
108+
pub(super) placeholder_field_indices: NodeMap<usize>,
107109
}
108110

109111
/// A unique identifier that we can use to lookup a definition

src/librustc_lint/builtin.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,22 @@ impl DeprecatedAttr {
669669
}
670670
}
671671

672+
fn lint_deprecated_attr(
673+
cx: &EarlyContext<'_>,
674+
attr: &ast::Attribute,
675+
msg: &str,
676+
suggestion: Option<&str>,
677+
) {
678+
cx.struct_span_lint(DEPRECATED, attr.span, &msg)
679+
.span_suggestion_short(
680+
attr.span,
681+
suggestion.unwrap_or("remove this attribute"),
682+
String::new(),
683+
Applicability::MachineApplicable
684+
)
685+
.emit();
686+
}
687+
672688
impl EarlyLintPass for DeprecatedAttr {
673689
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
674690
for &&(n, _, _, ref g) in &self.depr_attrs {
@@ -679,18 +695,15 @@ impl EarlyLintPass for DeprecatedAttr {
679695
_) = g {
680696
let msg = format!("use of deprecated attribute `{}`: {}. See {}",
681697
name, reason, link);
682-
let mut err = cx.struct_span_lint(DEPRECATED, attr.span, &msg);
683-
err.span_suggestion_short(
684-
attr.span,
685-
suggestion.unwrap_or("remove this attribute"),
686-
String::new(),
687-
Applicability::MachineApplicable
688-
);
689-
err.emit();
698+
lint_deprecated_attr(cx, attr, &msg, suggestion);
690699
}
691700
return;
692701
}
693702
}
703+
if attr.check_name(sym::no_start) || attr.check_name(sym::crate_id) {
704+
let msg = format!("use of deprecated attribute `{}`: no longer used.", attr.path);
705+
lint_deprecated_attr(cx, attr, &msg, None);
706+
}
694707
}
695708
}
696709

src/librustc_mir/interpret/operand.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
477477
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
478478
use rustc::mir::PlaceBase;
479479

480-
let mut op = match &place.base {
480+
let base_op = match &place.base {
481481
PlaceBase::Local(mir::RETURN_PLACE) =>
482482
throw_unsup!(ReadFromReturnPointer),
483483
PlaceBase::Local(local) => {
@@ -497,9 +497,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
497497
}
498498
};
499499

500-
for elem in place.projection.iter() {
501-
op = self.operand_projection(op, elem)?
502-
}
500+
let op = place.projection.iter().try_fold(
501+
base_op,
502+
|op, elem| self.operand_projection(op, elem)
503+
)?;
503504

504505
trace!("eval_place_to_op: got {:?}", *op);
505506
Ok(op)

src/librustc_resolve/macros.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ impl<'a> base::Resolver for Resolver<'a> {
237237
if let Res::Def(..) = res {
238238
self.session.span_err(
239239
span,
240-
"expected an inert attribute, found an attribute macro"
240+
&format!("expected an inert attribute, found {} {}",
241+
res.article(), res.descr()),
241242
);
242243
return Ok(InvocationRes::Single(self.dummy_ext(kind)));
243244
}
@@ -322,7 +323,7 @@ impl<'a> Resolver<'a> {
322323
self.check_stability_and_deprecation(&ext, path);
323324

324325
Ok(if ext.macro_kind() != kind {
325-
let expected = if kind == MacroKind::Attr { "attribute" } else { kind.descr() };
326+
let expected = if kind == MacroKind::Attr { "attribute" } else { kind.descr() };
326327
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path);
327328
self.session.struct_span_err(path.span, &msg)
328329
.span_label(path.span, format!("not {} {}", kind.article(), expected))

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ pub struct UnsafetyState {
400400

401401
impl UnsafetyState {
402402
pub fn function(unsafety: hir::Unsafety, def: hir::HirId) -> UnsafetyState {
403-
UnsafetyState { def: def, unsafety: unsafety, unsafe_push_count: 0, from_fn: true }
403+
UnsafetyState { def, unsafety, unsafe_push_count: 0, from_fn: true }
404404
}
405405

406406
pub fn recurse(&mut self, blk: &hir::Block) -> UnsafetyState {

src/test/ui/attributes/item-attributes.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#![rustc_dummy]
1212
#![rustc_dummy(attr5)]
1313

14-
#![crate_id="foobar#0.1"]
15-
1614
// These are attributes of the following mod
1715
#[rustc_dummy = "val"]
1816
#[rustc_dummy = "val"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
3+
struct S(
4+
#[rustfmt::skip] u8,
5+
u16,
6+
#[rustfmt::skip] u32,
7+
);
8+
9+
fn main() {}

src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@
8484
#![crate_name = "0900"]
8585
#![crate_type = "bin"] // cannot pass "0800" here
8686

87-
// For #![crate_id], see issue #43142. (I cannot bear to enshrine current behavior in a test)
87+
#![crate_id = "10"] //~ WARN use of deprecated attribute
8888

8989
// FIXME(#44232) we should warn that this isn't used.
9090
#![feature(rust1)]
9191

92-
// For #![no_start], see issue #43144. (I cannot bear to enshrine current behavior in a test)
92+
#![no_start] //~ WARN use of deprecated attribute
9393

9494
// (cannot easily gating state of crate-level #[no_main]; but non crate-level is below at "0400")
9595
#![no_builtins]

src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ LL | mod inner { #![macro_escape] }
186186
|
187187
= help: consider an outer attribute, `#[macro_use]` mod ...
188188

189+
warning: use of deprecated attribute `crate_id`: no longer used.
190+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:87:1
191+
|
192+
LL | #![crate_id = "10"]
193+
| ^^^^^^^^^^^^^^^^^^^ help: remove this attribute
194+
|
195+
= note: `#[warn(deprecated)]` on by default
196+
197+
warning: use of deprecated attribute `no_start`: no longer used.
198+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:92:1
199+
|
200+
LL | #![no_start]
201+
| ^^^^^^^^^^^^ help: remove this attribute
202+
189203
warning: the feature `rust1` has been stable since 1.0.0 and no longer requires an attribute to enable
190204
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:90:12
191205
|

src/test/ui/issues/issue-1251.rs

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#![feature(rustc_private)]
88

9-
#![crate_id="rust_get_test_int"]
10-
119
mod rustrt {
1210
extern crate libc;
1311

src/test/ui/issues/issue-49934-errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
fn foo<#[derive(Debug)] T>() {
22
//~^ ERROR `derive` may only be applied to structs, enums and unions
3-
//~| ERROR expected an inert attribute, found an attribute macro
3+
//~| ERROR expected an inert attribute, found a derive macro
44
match 0 {
55
#[derive(Debug)]
66
//~^ ERROR `derive` may only be applied to structs, enums and unions
7-
//~| ERROR expected an inert attribute, found an attribute macro
7+
//~| ERROR expected an inert attribute, found a derive macro
88
_ => (),
99
}
1010
}

src/test/ui/issues/issue-49934-errors.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: `derive` may only be applied to structs, enums and unions
44
LL | fn foo<#[derive(Debug)] T>() {
55
| ^^^^^^^^^^^^^^^^
66

7-
error: expected an inert attribute, found an attribute macro
7+
error: expected an inert attribute, found a derive macro
88
--> $DIR/issue-49934-errors.rs:1:17
99
|
1010
LL | fn foo<#[derive(Debug)] T>() {
@@ -16,7 +16,7 @@ error: `derive` may only be applied to structs, enums and unions
1616
LL | #[derive(Debug)]
1717
| ^^^^^^^^^^^^^^^^
1818

19-
error: expected an inert attribute, found an attribute macro
19+
error: expected an inert attribute, found a derive macro
2020
--> $DIR/issue-49934-errors.rs:5:18
2121
|
2222
LL | #[derive(Debug)]

src/test/ui/issues/issue-6919.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
// pretty-expanded FIXME #23616
66

7-
#![crate_id="issue-6919"]
87
extern crate issue6919_3;
98

109
pub fn main() {

0 commit comments

Comments
 (0)