Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 8 pull requests #93677

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a15cb49
#91836: Clarify error on casting larger integers to char
GKFX Dec 14, 2021
badb81a
#91939: integer to char cast error, make more targeted
GKFX Jan 25, 2022
73d0f7c
Remove "up here" arrow on item-infos
jsha Jan 7, 2022
0b7e1ba
Add trailing comma support
dtolnay Jan 21, 2022
8bdf08f
Change pp indent to signed to allow negative indents
dtolnay Jan 21, 2022
63406ac
Support offsetting the most recent break
dtolnay Jan 21, 2022
40fcbba
Change struct expr pretty printing to match rustfmt style
dtolnay Jan 21, 2022
ca3057f
Bless all pretty printer tests and ui tests
dtolnay Jan 22, 2022
8e99d92
Add regression tests for issue 80309
WaffleLapkin Feb 4, 2022
a3b1bc1
Specify min llvm version for issue 80309 regression tests
WaffleLapkin Feb 4, 2022
6756ff9
Update CPU idle tracking for apple hosts
Mark-Simulacrum Feb 4, 2022
8c6f7fd
Add new target armv7-unknown-linux-uclibceabi (softfloat)
lancethepants Feb 1, 2022
ca4296d
Refactor conditional
UltiRequiem Feb 4, 2022
bcf9884
resolve lifetimes for const generic defaults
compiler-errors Feb 5, 2022
8cdf44a
Rollup merge of #91939 - GKFX:feature-91866, r=cjgillot
matthiaskrgr Feb 5, 2022
f098a8e
Rollup merge of #92383 - lancethepants:armv7-unknown-linux-uclibceabi…
matthiaskrgr Feb 5, 2022
62fb88e
Rollup merge of #92651 - jsha:impl-spacing, r=GuillaumeGomez
matthiaskrgr Feb 5, 2022
39e2070
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
matthiaskrgr Feb 5, 2022
205f6e8
Rollup merge of #93649 - WaffleLapkin:regression_test_80309, r=oli-obk
matthiaskrgr Feb 5, 2022
7aceb80
Rollup merge of #93657 - Mark-Simulacrum:apple-measurement, r=pietroa…
matthiaskrgr Feb 5, 2022
fe34709
Rollup merge of #93659 - UltiRequiem:refactor_conditional_static_rust…
matthiaskrgr Feb 5, 2022
64eb1f4
Rollup merge of #93669 - compiler-errors:const-generic-args, r=lcnr
matthiaskrgr Feb 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions compiler/rustc_ast_pretty/src/pp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub enum Breaks {
Inconsistent,
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq)]
enum IndentStyle {
/// Vertically aligned under whatever column this block begins at.
///
Expand All @@ -164,19 +164,20 @@ enum IndentStyle {
Block { offset: isize },
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Default, PartialEq)]
pub struct BreakToken {
offset: isize,
blank_space: isize,
pre_break: Option<char>,
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq)]
pub struct BeginToken {
indent: IndentStyle,
breaks: Breaks,
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub enum Token {
// In practice a string token contains either a `&'static str` or a
// `String`. `Cow` is overkill for this because we never modify the data,
Expand Down Expand Up @@ -313,6 +314,12 @@ impl Printer {
}
}

pub fn offset(&mut self, offset: isize) {
if let Some(BufEntry { token: Token::Break(token), .. }) = &mut self.buf.last_mut() {
token.offset += offset;
}
}

fn check_stream(&mut self) {
while self.right_total - self.left_total > self.space {
if *self.scan_stack.front().unwrap() == self.buf.index_of_first() {
Expand Down Expand Up @@ -391,7 +398,9 @@ impl Printer {
if size > self.space {
self.print_stack.push(PrintFrame::Broken { indent: self.indent, breaks: token.breaks });
self.indent = match token.indent {
IndentStyle::Block { offset } => (self.indent as isize + offset) as usize,
IndentStyle::Block { offset } => {
usize::try_from(self.indent as isize + offset).unwrap()
}
IndentStyle::Visual => (MARGIN - self.space) as usize,
};
} else {
Expand All @@ -415,6 +424,9 @@ impl Printer {
self.pending_indentation += token.blank_space;
self.space -= token.blank_space;
} else {
if let Some(pre_break) = token.pre_break {
self.out.push(pre_break);
}
self.out.push('\n');
let indent = self.indent as isize + token.offset;
self.pending_indentation = indent;
Expand Down
33 changes: 23 additions & 10 deletions compiler/rustc_ast_pretty/src/pp/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ use std::borrow::Cow;

impl Printer {
/// "raw box"
pub fn rbox(&mut self, indent: usize, breaks: Breaks) {
self.scan_begin(BeginToken {
indent: IndentStyle::Block { offset: indent as isize },
breaks,
})
pub fn rbox(&mut self, indent: isize, breaks: Breaks) {
self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks })
}

/// Inconsistent breaking box
pub fn ibox(&mut self, indent: usize) {
pub fn ibox(&mut self, indent: isize) {
self.rbox(indent, Breaks::Inconsistent)
}

/// Consistent breaking box
pub fn cbox(&mut self, indent: usize) {
pub fn cbox(&mut self, indent: isize) {
self.rbox(indent, Breaks::Consistent)
}

Expand All @@ -25,7 +22,11 @@ impl Printer {
}

pub fn break_offset(&mut self, n: usize, off: isize) {
self.scan_break(BreakToken { offset: off, blank_space: n as isize })
self.scan_break(BreakToken {
offset: off,
blank_space: n as isize,
..BreakToken::default()
});
}

pub fn end(&mut self) {
Expand Down Expand Up @@ -66,12 +67,24 @@ impl Printer {
}

pub fn hardbreak_tok_offset(off: isize) -> Token {
Token::Break(BreakToken { offset: off, blank_space: SIZE_INFINITY })
Token::Break(BreakToken {
offset: off,
blank_space: SIZE_INFINITY,
..BreakToken::default()
})
}

pub fn trailing_comma(&mut self) {
self.scan_break(BreakToken {
blank_space: 1,
pre_break: Some(','),
..BreakToken::default()
});
}
}

impl Token {
pub fn is_hardbreak_tok(&self) -> bool {
matches!(self, Token::Break(BreakToken { offset: 0, blank_space: SIZE_INFINITY }))
*self == Printer::hardbreak_tok_offset(0)
}
}
5 changes: 4 additions & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod delimited;
mod expr;
mod item;

Expand All @@ -23,6 +24,8 @@ use rustc_span::{BytePos, FileName, Span};

use std::borrow::Cow;

pub use self::delimited::IterDelimited;

pub enum MacHeader<'a> {
Path(&'a ast::Path),
Keyword(&'static str),
Expand Down Expand Up @@ -92,7 +95,7 @@ pub struct State<'a> {
ann: &'a (dyn PpAnn + 'a),
}

crate const INDENT_UNIT: usize = 4;
crate const INDENT_UNIT: isize = 4;

/// Requires you to pass an input filename and reader so that
/// it can scan the input text for comments to copy forward.
Expand Down
41 changes: 41 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state/delimited.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::iter::Peekable;
use std::mem;
use std::ops::Deref;

pub struct Delimited<I: Iterator> {
is_first: bool,
iter: Peekable<I>,
}

pub trait IterDelimited: Iterator + Sized {
fn delimited(self) -> Delimited<Self> {
Delimited { is_first: true, iter: self.peekable() }
}
}

impl<I: Iterator> IterDelimited for I {}

pub struct IteratorItem<T> {
value: T,
pub is_first: bool,
pub is_last: bool,
}

impl<I: Iterator> Iterator for Delimited<I> {
type Item = IteratorItem<I::Item>;

fn next(&mut self) -> Option<Self::Item> {
let value = self.iter.next()?;
let is_first = mem::replace(&mut self.is_first, false);
let is_last = self.iter.peek().is_none();
Some(IteratorItem { value, is_first, is_last })
}
}

impl<T> Deref for IteratorItem<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.value
}
}
70 changes: 39 additions & 31 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::pp::Breaks::{Consistent, Inconsistent};
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
use crate::pp::Breaks::Inconsistent;
use crate::pprust::state::{AnnNode, IterDelimited, PrintState, State, INDENT_UNIT};

use rustc_ast::ptr::P;
use rustc_ast::util::parser::{self, AssocOp, Fixity};
Expand Down Expand Up @@ -117,38 +117,46 @@ impl<'a> State<'a> {
} else {
self.print_path(path, true, 0);
}
self.nbsp();
self.word("{");
self.commasep_cmnt(
Consistent,
fields,
|s, field| {
s.print_outer_attributes(&field.attrs);
s.ibox(INDENT_UNIT);
if !field.is_shorthand {
s.print_ident(field.ident);
s.word_space(":");
}
s.print_expr(&field.expr);
s.end();
},
|f| f.span,
);
match rest {
ast::StructRest::Base(_) | ast::StructRest::Rest(_) => {
self.ibox(INDENT_UNIT);
if !fields.is_empty() {
self.word(",");
self.space();
}
self.word("..");
if let ast::StructRest::Base(ref expr) = *rest {
self.print_expr(expr);
}
self.end();
let has_rest = match rest {
ast::StructRest::Base(_) | ast::StructRest::Rest(_) => true,
ast::StructRest::None => false,
};
if fields.is_empty() && !has_rest {
self.word("}");
return;
}
self.cbox(0);
for field in fields.iter().delimited() {
self.maybe_print_comment(field.span.hi());
self.print_outer_attributes(&field.attrs);
if field.is_first {
self.space_if_not_bol();
}
if !field.is_shorthand {
self.print_ident(field.ident);
self.word_nbsp(":");
}
self.print_expr(&field.expr);
if !field.is_last || has_rest {
self.word_space(",");
} else {
self.trailing_comma();
}
ast::StructRest::None if !fields.is_empty() => self.word(","),
_ => {}
}
if has_rest {
if fields.is_empty() {
self.space();
}
self.word("..");
if let ast::StructRest::Base(expr) = rest {
self.print_expr(expr);
}
self.space();
}
self.offset(-INDENT_UNIT);
self.end();
self.word("}");
}

Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_error_codes/src/error_codes/E0604.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ Erroneous code example:
0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
```

As the error message indicates, only `u8` can be cast into `char`. Example:
`char` is a Unicode Scalar Value, an integer value from 0 to 0xD7FF and
0xE000 to 0x10FFFF. (The gap is for surrogate pairs.) Only `u8` always fits in
those ranges so only `u8` may be cast to `char`.

To allow larger values, use `char::from_u32`, which checks the value is valid.

```
let c = 86u8 as char; // ok!
assert_eq!(c, 'V');
assert_eq!(86u8 as char, 'V'); // ok!
assert_eq!(char::from_u32(0x3B1), Some('α')); // ok!
assert_eq!(char::from_u32(0xD800), None); // not a USV.
```

For more information about casts, take a look at the Type cast section in
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'a> PrintState<'a> for State<'a> {
}
}

pub const INDENT_UNIT: usize = 4;
pub const INDENT_UNIT: isize = 4;

/// Requires you to pass an input filename and reader so that
/// it can scan the input text for comments to copy forward.
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,11 +1339,14 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
this.visit_ty(&ty);
}
}
GenericParamKind::Const { ref ty, .. } => {
GenericParamKind::Const { ref ty, default } => {
let was_in_const_generic = this.is_in_const_generic;
this.is_in_const_generic = true;
walk_list!(this, visit_param_bound, param.bounds);
this.visit_ty(&ty);
if let Some(default) = default {
this.visit_body(this.tcx.hir().body(default.body));
}
this.is_in_const_generic = was_in_const_generic;
}
}
Expand Down
23 changes: 23 additions & 0 deletions compiler/rustc_target/src/spec/armv7_unknown_linux_uclibceabi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::spec::{Target, TargetOptions};

// This target is for uclibc Linux on ARMv7 without NEON,
// thumb-mode or hardfloat.

pub fn target() -> Target {
let base = super::linux_uclibc_base::opts();
Target {
llvm_target: "armv7-unknown-linux-gnueabi".to_string(),
pointer_width: 32,
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
arch: "arm".to_string(),

options: TargetOptions {
features: "+v7,+thumb2,+soft-float,-neon".to_string(),
cpu: "generic".to_string(),
max_atomic_width: Some(64),
mcount: "_mcount".to_string(),
abi: "eabi".to_string(),
..base
},
}
}
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ supported_targets! {

("armv6k-nintendo-3ds", armv6k_nintendo_3ds),

("armv7-unknown-linux-uclibceabi", armv7_unknown_linux_uclibceabi),
("armv7-unknown-linux-uclibceabihf", armv7_unknown_linux_uclibceabihf),

("x86_64-unknown-none", x86_64_unknown_none),
Expand Down
20 changes: 16 additions & 4 deletions compiler/rustc_typeck/src/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,28 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.emit();
}
CastError::CastToChar => {
type_error_struct!(
let mut err = type_error_struct!(
fcx.tcx.sess,
self.span,
self.expr_ty,
E0604,
"only `u8` can be cast as `char`, not `{}`",
self.expr_ty
)
.span_label(self.span, "invalid cast")
.emit();
);
err.span_label(self.span, "invalid cast");
if self.expr_ty.is_numeric() {
err.span_help(
self.span,
if self.expr_ty == fcx.tcx.types.i8 {
"try casting from `u8` instead"
} else if self.expr_ty == fcx.tcx.types.u32 {
"try `char::from_u32` instead"
} else {
"try `char::from_u32` instead (via a `u32`)"
},
);
}
err.emit();
}
CastError::NonScalar => {
let mut err = type_error_struct!(
Expand Down
Loading