Skip to content

Commit 68a44c8

Browse files
authored
Rollup merge of #90856 - ken-matsui:suggestion-to-wrap-vec-allocator-api-in-tuple, r=davidtwco
Suggestion to wrap inner types using 'allocator_api' in tuple This PR provides a suggestion to wrap the inner types in tuple when being along with 'allocator_api'. Closes #83250 ```rust fn main() { let _vec: Vec<u8, _> = vec![]; //~ ERROR use of unstable library feature 'allocator_api' } ``` ```diff error[E0658]: use of unstable library feature 'allocator_api' --> $DIR/suggest-vec-allocator-api.rs:2:23 | LL | let _vec: Vec<u8, _> = vec![]; - | ^ + | ----^ + | | + | help: consider wrapping the inner types in tuple: `(u8, _)` | = note: see issue #32838 <#32838> for more information = help: add `#![feature(allocator_api)]` to the crate attributes to enable ```
2 parents 311fa1f + 57494f7 commit 68a44c8

File tree

5 files changed

+113
-8
lines changed

5 files changed

+113
-8
lines changed

compiler/rustc_middle/src/middle/stability.rs

+53-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
pub use self::StabilityLevel::*;
55

6-
use crate::ty::{self, TyCtxt};
6+
use crate::ty::{self, DefIdTree, TyCtxt};
77
use rustc_ast::NodeId;
88
use rustc_attr::{self as attr, ConstStability, Deprecation, Stability};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -90,6 +90,7 @@ pub fn report_unstable(
9090
feature: Symbol,
9191
reason: Option<Symbol>,
9292
issue: Option<NonZeroU32>,
93+
suggestion: Option<(Span, String, String, Applicability)>,
9394
is_soft: bool,
9495
span: Span,
9596
soft_handler: impl FnOnce(&'static Lint, Span, &str),
@@ -116,8 +117,12 @@ pub fn report_unstable(
116117
if is_soft {
117118
soft_handler(SOFT_UNSTABLE, span, &msg)
118119
} else {
119-
feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg)
120-
.emit();
120+
let mut err =
121+
feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg);
122+
if let Some((inner_types, ref msg, sugg, applicability)) = suggestion {
123+
err.span_suggestion(inner_types, msg, sugg, applicability);
124+
}
125+
err.emit();
121126
}
122127
}
123128
}
@@ -271,7 +276,13 @@ pub enum EvalResult {
271276
Allow,
272277
/// We cannot use the item because it is unstable and we did not provide the
273278
/// corresponding feature gate.
274-
Deny { feature: Symbol, reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool },
279+
Deny {
280+
feature: Symbol,
281+
reason: Option<Symbol>,
282+
issue: Option<NonZeroU32>,
283+
suggestion: Option<(Span, String, String, Applicability)>,
284+
is_soft: bool,
285+
},
275286
/// The item does not have the `#[stable]` or `#[unstable]` marker assigned.
276287
Unmarked,
277288
}
@@ -292,6 +303,32 @@ fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
292303
}
293304
}
294305

306+
// See issue #83250.
307+
fn suggestion_for_allocator_api(
308+
tcx: TyCtxt<'_>,
309+
def_id: DefId,
310+
span: Span,
311+
feature: Symbol,
312+
) -> Option<(Span, String, String, Applicability)> {
313+
if feature == sym::allocator_api {
314+
if let Some(trait_) = tcx.parent(def_id) {
315+
if tcx.is_diagnostic_item(sym::Vec, trait_) {
316+
let sm = tcx.sess.parse_sess.source_map();
317+
let inner_types = sm.span_extend_to_prev_char(span, '<', true);
318+
if let Ok(snippet) = sm.span_to_snippet(inner_types) {
319+
return Some((
320+
inner_types,
321+
"consider wrapping the inner types in tuple".to_string(),
322+
format!("({})", snippet),
323+
Applicability::MaybeIncorrect,
324+
));
325+
}
326+
}
327+
}
328+
}
329+
None
330+
}
331+
295332
impl<'tcx> TyCtxt<'tcx> {
296333
/// Evaluates the stability of an item.
297334
///
@@ -406,7 +443,8 @@ impl<'tcx> TyCtxt<'tcx> {
406443
}
407444
}
408445

409-
EvalResult::Deny { feature, reason, issue, is_soft }
446+
let suggestion = suggestion_for_allocator_api(self, def_id, span, feature);
447+
EvalResult::Deny { feature, reason, issue, suggestion, is_soft }
410448
}
411449
Some(_) => {
412450
// Stable APIs are always ok to call and deprecated APIs are
@@ -457,9 +495,16 @@ impl<'tcx> TyCtxt<'tcx> {
457495
};
458496
match self.eval_stability(def_id, id, span, method_span) {
459497
EvalResult::Allow => {}
460-
EvalResult::Deny { feature, reason, issue, is_soft } => {
461-
report_unstable(self.sess, feature, reason, issue, is_soft, span, soft_handler)
462-
}
498+
EvalResult::Deny { feature, reason, issue, suggestion, is_soft } => report_unstable(
499+
self.sess,
500+
feature,
501+
reason,
502+
issue,
503+
suggestion,
504+
is_soft,
505+
span,
506+
soft_handler,
507+
),
463508
EvalResult::Unmarked => unmarked(span, def_id),
464509
}
465510
}

compiler/rustc_resolve/src/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ impl<'a> Resolver<'a> {
11331133
feature,
11341134
reason,
11351135
issue,
1136+
None,
11361137
is_soft,
11371138
span,
11381139
soft_handler,

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ symbols! {
307307
alloc_layout,
308308
alloc_zeroed,
309309
allocator,
310+
allocator_api,
310311
allocator_internals,
311312
allow,
312313
allow_fail,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let _: Vec<u8, _> = vec![]; //~ ERROR use of unstable library feature 'allocator_api'
3+
#[rustfmt::skip]
4+
let _: Vec<
5+
String,
6+
_> = vec![]; //~ ERROR use of unstable library feature 'allocator_api'
7+
let _ = Vec::<u16, _>::new(); //~ ERROR use of unstable library feature 'allocator_api'
8+
let _boxed: Box<u32, _> = Box::new(10); //~ ERROR use of unstable library feature 'allocator_api'
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0658]: use of unstable library feature 'allocator_api'
2+
--> $DIR/suggest-vec-allocator-api.rs:2:20
3+
|
4+
LL | let _: Vec<u8, _> = vec![];
5+
| ----^
6+
| |
7+
| help: consider wrapping the inner types in tuple: `(u8, _)`
8+
|
9+
= note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information
10+
= help: add `#![feature(allocator_api)]` to the crate attributes to enable
11+
12+
error[E0658]: use of unstable library feature 'allocator_api'
13+
--> $DIR/suggest-vec-allocator-api.rs:6:9
14+
|
15+
LL | _> = vec![];
16+
| ^
17+
|
18+
= note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information
19+
= help: add `#![feature(allocator_api)]` to the crate attributes to enable
20+
help: consider wrapping the inner types in tuple
21+
|
22+
LL ~ let _: Vec<(
23+
LL + String,
24+
LL ~ _)> = vec![];
25+
|
26+
27+
error[E0658]: use of unstable library feature 'allocator_api'
28+
--> $DIR/suggest-vec-allocator-api.rs:8:26
29+
|
30+
LL | let _boxed: Box<u32, _> = Box::new(10);
31+
| ^
32+
|
33+
= note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information
34+
= help: add `#![feature(allocator_api)]` to the crate attributes to enable
35+
36+
error[E0658]: use of unstable library feature 'allocator_api'
37+
--> $DIR/suggest-vec-allocator-api.rs:7:24
38+
|
39+
LL | let _ = Vec::<u16, _>::new();
40+
| -----^
41+
| |
42+
| help: consider wrapping the inner types in tuple: `(u16, _)`
43+
|
44+
= note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information
45+
= help: add `#![feature(allocator_api)]` to the crate attributes to enable
46+
47+
error: aborting due to 4 previous errors
48+
49+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)