|
14 | 14 | //! conflicts between multiple such attributes attached to the same
|
15 | 15 | //! item.
|
16 | 16 |
|
17 |
| -use syntax_pos::Span; |
| 17 | +use syntax_pos::{BytePos, Span}; |
18 | 18 | use ty::TyCtxt;
|
19 | 19 |
|
20 | 20 | use hir;
|
@@ -156,17 +156,54 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
|
156 | 156 | .filter(|attr| attr.name() == "repr")
|
157 | 157 | .filter_map(|attr| {
|
158 | 158 | let list = attr.meta_item_list();
|
159 |
| - let mut has_hints = false; |
160 |
| - if let Some(ref list) = list { |
161 |
| - has_hints = !list.is_empty(); |
162 |
| - } |
| 159 | + |
| 160 | + // Emit warnings with `repr` either has a literal assignment (`#[repr = "C"]`) or |
| 161 | + // no hints (``#[repr]`) |
| 162 | + let has_hints = list.as_ref().map(|ref list| !list.is_empty()).unwrap_or(false); |
163 | 163 | if !has_hints {
|
164 |
| - span_warn!( |
165 |
| - self.tcx.sess, |
166 |
| - item.span, |
167 |
| - E0689, |
168 |
| - "`repr` attribute cannot be empty", |
169 |
| - ); |
| 164 | + let mut suggested = false; |
| 165 | + let mut warn = if let Some(ref lit) = attr.value_str() { |
| 166 | + // avoid warning about empty `repr` on `#[repr = "foo"]` |
| 167 | + let sp = match format!("{}", lit).as_ref() { |
| 168 | + "C" | "packed" | "rust" | "u*" | "i*" => { |
| 169 | + let lo = attr.span.lo() + BytePos(2); |
| 170 | + let hi = attr.span.hi() - BytePos(1); |
| 171 | + suggested = true; |
| 172 | + attr.span.with_lo(lo).with_hi(hi) |
| 173 | + } |
| 174 | + _ => attr.span, // the literal wasn't a valid `repr` arg |
| 175 | + }; |
| 176 | + let mut warn = self.tcx.sess.struct_span_warn( |
| 177 | + sp, |
| 178 | + "`repr` attribute isn't configurable with a literal", |
| 179 | + ); |
| 180 | + if suggested { |
| 181 | + // if the literal could have been a valid `repr` arg, |
| 182 | + // suggest the correct syntax |
| 183 | + warn.span_suggestion( |
| 184 | + sp, |
| 185 | + "give `repr` a hint", |
| 186 | + format!("repr({})", lit), |
| 187 | + ); |
| 188 | + } else { |
| 189 | + warn.span_label(attr.span, "needs a hint"); |
| 190 | + } |
| 191 | + warn |
| 192 | + } else { |
| 193 | + let mut warn = self.tcx.sess.struct_span_warn( |
| 194 | + attr.span, |
| 195 | + "`repr` attribute must have a hint", |
| 196 | + ); |
| 197 | + warn.span_label(attr.span, "needs a hint"); |
| 198 | + warn |
| 199 | + }; |
| 200 | + if !suggested { |
| 201 | + warn.help("valid hints include `#[repr(C)]`, `#[repr(packed)]` and \ |
| 202 | + `#[repr(rust)]`"); |
| 203 | + warn.note("for more information, visit \ |
| 204 | + <https://doc.rust-lang.org/nomicon/other-reprs.html>"); |
| 205 | + } |
| 206 | + warn.emit(); |
170 | 207 | }
|
171 | 208 | list
|
172 | 209 | })
|
|
0 commit comments