mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-24 05:33:41 +00:00
Auto merge of #14837 - Veykril:rustc-lexer, r=Veykril
Support c string literals
This commit is contained in:
commit
9ce95674e8
@ -611,6 +611,7 @@ impl<'a> Printer<'a> {
|
||||
match literal {
|
||||
Literal::String(it) => w!(self, "{:?}", it),
|
||||
Literal::ByteString(it) => w!(self, "\"{}\"", it.escape_ascii()),
|
||||
Literal::CString(it) => w!(self, "\"{}\\0\"", it),
|
||||
Literal::Char(it) => w!(self, "'{}'", it.escape_debug()),
|
||||
Literal::Bool(it) => w!(self, "{}", it),
|
||||
Literal::Int(i, suffix) => {
|
||||
|
@ -85,6 +85,7 @@ impl fmt::Display for FloatTypeWrapper {
|
||||
pub enum Literal {
|
||||
String(Box<str>),
|
||||
ByteString(Box<[u8]>),
|
||||
CString(Box<str>),
|
||||
Char(char),
|
||||
Bool(bool),
|
||||
Int(i128, Option<BuiltinInt>),
|
||||
@ -135,6 +136,10 @@ impl From<ast::LiteralKind> for Literal {
|
||||
let text = s.value().map(Box::from).unwrap_or_else(Default::default);
|
||||
Literal::String(text)
|
||||
}
|
||||
LiteralKind::CString(s) => {
|
||||
let text = s.value().map(Box::from).unwrap_or_else(Default::default);
|
||||
Literal::CString(text)
|
||||
}
|
||||
LiteralKind::Byte(b) => {
|
||||
Literal::Uint(b.value().unwrap_or_default() as u128, Some(BuiltinUint::U8))
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ pub enum GenericRequirement {
|
||||
|
||||
macro_rules! language_item_table {
|
||||
(
|
||||
$( $(#[$attr:meta])* $variant:ident, $name:ident, $method:ident, $target:expr, $generics:expr; )*
|
||||
$( $(#[$attr:meta])* $variant:ident, $module:ident :: $name:ident, $method:ident, $target:expr, $generics:expr; )*
|
||||
) => {
|
||||
|
||||
/// A representation of all the valid language items in Rust.
|
||||
@ -244,82 +244,86 @@ impl LangItem {
|
||||
|
||||
language_item_table! {
|
||||
// Variant name, Name, Getter method name, Target Generic requirements;
|
||||
Sized, sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Unsize, unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
Sized, sym::sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
|
||||
StructuralPeq, structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
|
||||
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
|
||||
/// Trait injected by `#[derive(Eq)]`, (i.e. "Total EQ"; no, I will not apologize).
|
||||
StructuralTeq, structural_teq, structural_teq_trait, Target::Trait, GenericRequirement::None;
|
||||
Copy, copy, copy_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Clone, clone, clone_trait, Target::Trait, GenericRequirement::None;
|
||||
Sync, sync, sync_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DiscriminantKind, discriminant_kind, discriminant_kind_trait, Target::Trait, GenericRequirement::None;
|
||||
StructuralTeq, sym::structural_teq, structural_teq_trait, Target::Trait, GenericRequirement::None;
|
||||
Copy, sym::copy, copy_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Clone, sym::clone, clone_trait, Target::Trait, GenericRequirement::None;
|
||||
Sync, sym::sync, sync_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DiscriminantKind, sym::discriminant_kind, discriminant_kind_trait, Target::Trait, GenericRequirement::None;
|
||||
/// The associated item of the [`DiscriminantKind`] trait.
|
||||
Discriminant, discriminant_type, discriminant_type, Target::AssocTy, GenericRequirement::None;
|
||||
Discriminant, sym::discriminant_type, discriminant_type, Target::AssocTy, GenericRequirement::None;
|
||||
|
||||
PointeeTrait, pointee_trait, pointee_trait, Target::Trait, GenericRequirement::None;
|
||||
Metadata, metadata_type, metadata_type, Target::AssocTy, GenericRequirement::None;
|
||||
DynMetadata, dyn_metadata, dyn_metadata, Target::Struct, GenericRequirement::None;
|
||||
PointeeTrait, sym::pointee_trait, pointee_trait, Target::Trait, GenericRequirement::None;
|
||||
Metadata, sym::metadata_type, metadata_type, Target::AssocTy, GenericRequirement::None;
|
||||
DynMetadata, sym::dyn_metadata, dyn_metadata, Target::Struct, GenericRequirement::None;
|
||||
|
||||
Freeze, freeze, freeze_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Freeze, sym::freeze, freeze_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
|
||||
Drop, drop, drop_trait, Target::Trait, GenericRequirement::None;
|
||||
Destruct, destruct, destruct_trait, Target::Trait, GenericRequirement::None;
|
||||
FnPtrTrait, sym::fn_ptr_trait, fn_ptr_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
FnPtrAddr, sym::fn_ptr_addr, fn_ptr_addr, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
|
||||
CoerceUnsized, coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
DispatchFromDyn, dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
Drop, sym::drop, drop_trait, Target::Trait, GenericRequirement::None;
|
||||
Destruct, sym::destruct, destruct_trait, Target::Trait, GenericRequirement::None;
|
||||
|
||||
CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
DispatchFromDyn, sym::dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
|
||||
// language items relating to transmutability
|
||||
TransmuteOpts, transmute_opts, transmute_opts, Target::Struct, GenericRequirement::Exact(0);
|
||||
TransmuteTrait, transmute_trait, transmute_trait, Target::Trait, GenericRequirement::Exact(3);
|
||||
TransmuteOpts, sym::transmute_opts, transmute_opts, Target::Struct, GenericRequirement::Exact(0);
|
||||
TransmuteTrait, sym::transmute_trait, transmute_trait, Target::Trait, GenericRequirement::Exact(3);
|
||||
|
||||
Add, add, add_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Sub, sub, sub_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Mul, mul, mul_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Div, div, div_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Rem, rem, rem_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Neg, neg, neg_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Not, not, not_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
BitXor, bitxor, bitxor_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitAnd, bitand, bitand_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitOr, bitor, bitor_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Shl, shl, shl_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Shr, shr, shr_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
AddAssign, add_assign, add_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
SubAssign, sub_assign, sub_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
MulAssign, mul_assign, mul_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
DivAssign, div_assign, div_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
RemAssign, rem_assign, rem_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitXorAssign, bitxor_assign, bitxor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitAndAssign, bitand_assign, bitand_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitOrAssign, bitor_assign, bitor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
ShlAssign, shl_assign, shl_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
ShrAssign, shr_assign, shr_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Index, index, index_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
IndexMut, index_mut, index_mut_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Add, sym::add, add_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Sub, sym::sub, sub_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Mul, sym::mul, mul_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Div, sym::div, div_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Rem, sym::rem, rem_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Neg, sym::neg, neg_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Not, sym::not, not_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
BitXor, sym::bitxor, bitxor_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitAnd, sym::bitand, bitand_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitOr, sym::bitor, bitor_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Shl, sym::shl, shl_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Shr, sym::shr, shr_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
AddAssign, sym::add_assign, add_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
SubAssign, sym::sub_assign, sub_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
MulAssign, sym::mul_assign, mul_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
DivAssign, sym::div_assign, div_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
RemAssign, sym::rem_assign, rem_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitXorAssign, sym::bitxor_assign, bitxor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitAndAssign, sym::bitand_assign, bitand_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
BitOrAssign, sym::bitor_assign, bitor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
ShlAssign, sym::shl_assign, shl_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
ShrAssign, sym::shr_assign, shr_assign_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Index, sym::index, index_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
IndexMut, sym::index_mut, index_mut_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
|
||||
UnsafeCell, unsafe_cell, unsafe_cell_type, Target::Struct, GenericRequirement::None;
|
||||
VaList, va_list, va_list, Target::Struct, GenericRequirement::None;
|
||||
UnsafeCell, sym::unsafe_cell, unsafe_cell_type, Target::Struct, GenericRequirement::None;
|
||||
VaList, sym::va_list, va_list, Target::Struct, GenericRequirement::None;
|
||||
|
||||
Deref, deref, deref_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DerefMut, deref_mut, deref_mut_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DerefTarget, deref_target, deref_target, Target::AssocTy, GenericRequirement::None;
|
||||
Receiver, receiver, receiver_trait, Target::Trait, GenericRequirement::None;
|
||||
Deref, sym::deref, deref_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DerefMut, sym::deref_mut, deref_mut_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DerefTarget, sym::deref_target, deref_target, Target::AssocTy, GenericRequirement::None;
|
||||
Receiver, sym::receiver, receiver_trait, Target::Trait, GenericRequirement::None;
|
||||
|
||||
Fn, fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
FnMut, fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
FnOnce, fn_once, fn_once_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
Fn, kw::fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
FnMut, sym::fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
FnOnce, sym::fn_once, fn_once_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
|
||||
FnOnceOutput, fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;
|
||||
FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;
|
||||
|
||||
Future, future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
GeneratorState, generator_state, gen_state, Target::Enum, GenericRequirement::None;
|
||||
Generator, generator, gen_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
Unpin, unpin, unpin_trait, Target::Trait, GenericRequirement::None;
|
||||
Pin, pin, pin_type, Target::Struct, GenericRequirement::None;
|
||||
Future, sym::future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
GeneratorState, sym::generator_state, gen_state, Target::Enum, GenericRequirement::None;
|
||||
Generator, sym::generator, gen_trait, Target::Trait, GenericRequirement::Minimum(1);
|
||||
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;
|
||||
Pin, sym::pin, pin_type, Target::Struct, GenericRequirement::None;
|
||||
|
||||
PartialEq, eq, eq_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
PartialOrd, partial_ord, partial_ord_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
PartialEq, sym::eq, eq_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
PartialOrd, sym::partial_ord, partial_ord_trait, Target::Trait, GenericRequirement::Exact(1);
|
||||
CVoid, sym::c_void, c_void, Target::Enum, GenericRequirement::None;
|
||||
|
||||
// A number of panic-related lang items. The `panic` item corresponds to divide-by-zero and
|
||||
// various panic cases with `match`. The `panic_bounds_check` item is for indexing arrays.
|
||||
@ -328,93 +332,103 @@ language_item_table! {
|
||||
// in the sense that a crate is not required to have it defined to use it, but a final product
|
||||
// is required to define it somewhere. Additionally, there are restrictions on crates that use
|
||||
// a weak lang item, but do not have it defined.
|
||||
Panic, panic, panic_fn, Target::Fn, GenericRequirement::Exact(0);
|
||||
PanicNounwind, panic_nounwind, panic_nounwind, Target::Fn, GenericRequirement::Exact(0);
|
||||
PanicFmt, panic_fmt, panic_fmt, Target::Fn, GenericRequirement::None;
|
||||
PanicDisplay, panic_display, panic_display, Target::Fn, GenericRequirement::None;
|
||||
ConstPanicFmt, const_panic_fmt, const_panic_fmt, Target::Fn, GenericRequirement::None;
|
||||
PanicBoundsCheck, panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::Exact(0);
|
||||
PanicInfo, panic_info, panic_info, Target::Struct, GenericRequirement::None;
|
||||
PanicLocation, panic_location, panic_location, Target::Struct, GenericRequirement::None;
|
||||
PanicImpl, panic_impl, panic_impl, Target::Fn, GenericRequirement::None;
|
||||
PanicCannotUnwind, panic_cannot_unwind, panic_cannot_unwind, Target::Fn, GenericRequirement::Exact(0);
|
||||
Panic, sym::panic, panic_fn, Target::Fn, GenericRequirement::Exact(0);
|
||||
PanicNounwind, sym::panic_nounwind, panic_nounwind, Target::Fn, GenericRequirement::Exact(0);
|
||||
PanicFmt, sym::panic_fmt, panic_fmt, Target::Fn, GenericRequirement::None;
|
||||
PanicDisplay, sym::panic_display, panic_display, Target::Fn, GenericRequirement::None;
|
||||
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn, GenericRequirement::None;
|
||||
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::Exact(0);
|
||||
PanicMisalignedPointerDereference, sym::panic_misaligned_pointer_dereference, panic_misaligned_pointer_dereference_fn, Target::Fn, GenericRequirement::Exact(0);
|
||||
PanicInfo, sym::panic_info, panic_info, Target::Struct, GenericRequirement::None;
|
||||
PanicLocation, sym::panic_location, panic_location, Target::Struct, GenericRequirement::None;
|
||||
PanicImpl, sym::panic_impl, panic_impl, Target::Fn, GenericRequirement::None;
|
||||
PanicCannotUnwind, sym::panic_cannot_unwind, panic_cannot_unwind, Target::Fn, GenericRequirement::Exact(0);
|
||||
/// libstd panic entry point. Necessary for const eval to be able to catch it
|
||||
BeginPanic, begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
|
||||
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
|
||||
|
||||
ExchangeMalloc, exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
|
||||
BoxFree, box_free, box_free_fn, Target::Fn, GenericRequirement::Minimum(1);
|
||||
DropInPlace, drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
|
||||
AllocLayout, alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
|
||||
// Lang items needed for `format_args!()`.
|
||||
FormatAlignment, sym::format_alignment, format_alignment, Target::Enum, GenericRequirement::None;
|
||||
FormatArgument, sym::format_argument, format_argument, Target::Struct, GenericRequirement::None;
|
||||
FormatArguments, sym::format_arguments, format_arguments, Target::Struct, GenericRequirement::None;
|
||||
FormatCount, sym::format_count, format_count, Target::Enum, GenericRequirement::None;
|
||||
FormatPlaceholder, sym::format_placeholder, format_placeholder, Target::Struct, GenericRequirement::None;
|
||||
FormatUnsafeArg, sym::format_unsafe_arg, format_unsafe_arg, Target::Struct, GenericRequirement::None;
|
||||
|
||||
Start, start, start_fn, Target::Fn, GenericRequirement::Exact(1);
|
||||
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
|
||||
BoxFree, sym::box_free, box_free_fn, Target::Fn, GenericRequirement::Minimum(1);
|
||||
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
|
||||
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
|
||||
|
||||
EhPersonality, eh_personality, eh_personality, Target::Fn, GenericRequirement::None;
|
||||
EhCatchTypeinfo, eh_catch_typeinfo, eh_catch_typeinfo, Target::Static, GenericRequirement::None;
|
||||
Start, sym::start, start_fn, Target::Fn, GenericRequirement::Exact(1);
|
||||
|
||||
OwnedBox, owned_box, owned_box, Target::Struct, GenericRequirement::Minimum(1);
|
||||
EhPersonality, sym::eh_personality, eh_personality, Target::Fn, GenericRequirement::None;
|
||||
EhCatchTypeinfo, sym::eh_catch_typeinfo, eh_catch_typeinfo, Target::Static, GenericRequirement::None;
|
||||
|
||||
PhantomData, phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1);
|
||||
OwnedBox, sym::owned_box, owned_box, Target::Struct, GenericRequirement::Minimum(1);
|
||||
|
||||
ManuallyDrop, manually_drop, manually_drop, Target::Struct, GenericRequirement::None;
|
||||
PhantomData, sym::phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1);
|
||||
|
||||
MaybeUninit, maybe_uninit, maybe_uninit, Target::Union, GenericRequirement::None;
|
||||
ManuallyDrop, sym::manually_drop, manually_drop, Target::Struct, GenericRequirement::None;
|
||||
|
||||
MaybeUninit, sym::maybe_uninit, maybe_uninit, Target::Union, GenericRequirement::None;
|
||||
|
||||
/// Align offset for stride != 1; must not panic.
|
||||
AlignOffset, align_offset, align_offset_fn, Target::Fn, GenericRequirement::None;
|
||||
AlignOffset, sym::align_offset, align_offset_fn, Target::Fn, GenericRequirement::None;
|
||||
|
||||
Termination, termination, termination, Target::Trait, GenericRequirement::None;
|
||||
Termination, sym::termination, termination, Target::Trait, GenericRequirement::None;
|
||||
|
||||
Try, Try, try_trait, Target::Trait, GenericRequirement::None;
|
||||
Try, sym::Try, try_trait, Target::Trait, GenericRequirement::None;
|
||||
|
||||
Tuple, tuple_trait, tuple_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
Tuple, sym::tuple_trait, tuple_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
|
||||
SliceLen, slice_len_fn, slice_len_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None;
|
||||
SliceLen, sym::slice_len_fn, slice_len_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None;
|
||||
|
||||
// Language items from AST lowering
|
||||
TryTraitFromResidual, from_residual, from_residual_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
TryTraitFromOutput, from_output, from_output_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
TryTraitBranch, branch, branch_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
TryTraitFromYeet, from_yeet, from_yeet_fn, Target::Fn, GenericRequirement::None;
|
||||
TryTraitFromResidual, sym::from_residual, from_residual_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
TryTraitFromOutput, sym::from_output, from_output_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
TryTraitBranch, sym::branch, branch_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
TryTraitFromYeet, sym::from_yeet, from_yeet_fn, Target::Fn, GenericRequirement::None;
|
||||
|
||||
PointerSized, pointer_sized, pointer_sized, Target::Trait, GenericRequirement::Exact(0);
|
||||
PointerLike, sym::pointer_like, pointer_like, Target::Trait, GenericRequirement::Exact(0);
|
||||
|
||||
Poll, Poll, poll, Target::Enum, GenericRequirement::None;
|
||||
PollReady, Ready, poll_ready_variant, Target::Variant, GenericRequirement::None;
|
||||
PollPending, Pending, poll_pending_variant, Target::Variant, GenericRequirement::None;
|
||||
ConstParamTy, sym::const_param_ty, const_param_ty_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
|
||||
Poll, sym::Poll, poll, Target::Enum, GenericRequirement::None;
|
||||
PollReady, sym::Ready, poll_ready_variant, Target::Variant, GenericRequirement::None;
|
||||
PollPending, sym::Pending, poll_pending_variant, Target::Variant, GenericRequirement::None;
|
||||
|
||||
// FIXME(swatinem): the following lang items are used for async lowering and
|
||||
// should become obsolete eventually.
|
||||
ResumeTy, ResumeTy, resume_ty, Target::Struct, GenericRequirement::None;
|
||||
IdentityFuture, identity_future, identity_future_fn, Target::Fn, GenericRequirement::None;
|
||||
GetContext, get_context, get_context_fn, Target::Fn, GenericRequirement::None;
|
||||
ResumeTy, sym::ResumeTy, resume_ty, Target::Struct, GenericRequirement::None;
|
||||
GetContext, sym::get_context, get_context_fn, Target::Fn, GenericRequirement::None;
|
||||
|
||||
Context, Context, context, Target::Struct, GenericRequirement::None;
|
||||
FuturePoll, poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
Context, sym::Context, context, Target::Struct, GenericRequirement::None;
|
||||
FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
|
||||
FromFrom, from, from_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
Option, sym::Option, option_type, Target::Enum, GenericRequirement::None;
|
||||
OptionSome, sym::Some, option_some_variant, Target::Variant, GenericRequirement::None;
|
||||
OptionNone, sym::None, option_none_variant, Target::Variant, GenericRequirement::None;
|
||||
|
||||
OptionSome, Some, option_some_variant, Target::Variant, GenericRequirement::None;
|
||||
OptionNone, None, option_none_variant, Target::Variant, GenericRequirement::None;
|
||||
ResultOk, sym::Ok, result_ok_variant, Target::Variant, GenericRequirement::None;
|
||||
ResultErr, sym::Err, result_err_variant, Target::Variant, GenericRequirement::None;
|
||||
|
||||
ResultOk, Ok, result_ok_variant, Target::Variant, GenericRequirement::None;
|
||||
ResultErr, Err, result_err_variant, Target::Variant, GenericRequirement::None;
|
||||
ControlFlowContinue, sym::Continue, cf_continue_variant, Target::Variant, GenericRequirement::None;
|
||||
ControlFlowBreak, sym::Break, cf_break_variant, Target::Variant, GenericRequirement::None;
|
||||
|
||||
ControlFlowContinue, Continue, cf_continue_variant, Target::Variant, GenericRequirement::None;
|
||||
ControlFlowBreak, Break, cf_break_variant, Target::Variant, GenericRequirement::None;
|
||||
IntoFutureIntoFuture, sym::into_future, into_future_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
IntoIterIntoIter, sym::into_iter, into_iter_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
IteratorNext, sym::next, next_fn, Target::Method(MethodKind::Trait { body: false}), GenericRequirement::None;
|
||||
|
||||
IntoFutureIntoFuture, into_future, into_future_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
IntoIterIntoIter, into_iter, into_iter_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
IteratorNext, next, next_fn, Target::Method(MethodKind::Trait { body: false}), GenericRequirement::None;
|
||||
PinNewUnchecked, sym::new_unchecked, new_unchecked_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None;
|
||||
|
||||
PinNewUnchecked, new_unchecked, new_unchecked_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None;
|
||||
RangeFrom, sym::RangeFrom, range_from_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeFull, sym::RangeFull, range_full_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeInclusiveStruct, sym::RangeInclusive, range_inclusive_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeInclusiveNew, sym::range_inclusive_new, range_inclusive_new_method, Target::Method(MethodKind::Inherent), GenericRequirement::None;
|
||||
Range, sym::Range, range_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeToInclusive, sym::RangeToInclusive, range_to_inclusive_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeTo, sym::RangeTo, range_to_struct, Target::Struct, GenericRequirement::None;
|
||||
|
||||
RangeFrom, RangeFrom, range_from_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeFull, RangeFull, range_full_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeInclusiveStruct, RangeInclusive, range_inclusive_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeInclusiveNew, range_inclusive_new, range_inclusive_new_method, Target::Method(MethodKind::Inherent), GenericRequirement::None;
|
||||
Range, Range, range_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeToInclusive, RangeToInclusive, range_to_inclusive_struct, Target::Struct, GenericRequirement::None;
|
||||
RangeTo, RangeTo, range_to_struct, Target::Struct, GenericRequirement::None;
|
||||
|
||||
String, String, string, Target::Struct, GenericRequirement::None;
|
||||
String, sym::String, string, Target::Struct, GenericRequirement::None;
|
||||
CStr, sym::CStr, c_str, Target::Struct, GenericRequirement::None;
|
||||
}
|
||||
|
@ -1883,6 +1883,38 @@ fn byte_string() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn c_string() {
|
||||
check_number(
|
||||
r#"
|
||||
//- minicore: index, slice
|
||||
#[lang = "CStr"]
|
||||
pub struct CStr {
|
||||
inner: [u8]
|
||||
}
|
||||
const GOAL: u8 = {
|
||||
let a = c"hello";
|
||||
a.inner[0]
|
||||
};
|
||||
"#,
|
||||
104,
|
||||
);
|
||||
check_number(
|
||||
r#"
|
||||
//- minicore: index, slice
|
||||
#[lang = "CStr"]
|
||||
pub struct CStr {
|
||||
inner: [u8]
|
||||
}
|
||||
const GOAL: u8 = {
|
||||
let a = c"hello";
|
||||
a.inner[6]
|
||||
};
|
||||
"#,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn consts() {
|
||||
check_number(
|
||||
|
@ -13,7 +13,7 @@ use hir_def::{
|
||||
hir::{
|
||||
ArithOp, Array, BinaryOp, ClosureKind, Expr, ExprId, LabelId, Literal, Statement, UnaryOp,
|
||||
},
|
||||
lang_item::LangItem,
|
||||
lang_item::{LangItem, LangItemTarget},
|
||||
path::{GenericArg, GenericArgs},
|
||||
BlockId, ConstParamId, FieldId, ItemContainerId, Lookup,
|
||||
};
|
||||
@ -832,6 +832,20 @@ impl<'a> InferenceContext<'a> {
|
||||
let array_type = TyKind::Array(byte_type, len).intern(Interner);
|
||||
TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(Interner)
|
||||
}
|
||||
Literal::CString(..) => TyKind::Ref(
|
||||
Mutability::Not,
|
||||
static_lifetime(),
|
||||
self.resolve_lang_item(LangItem::CStr)
|
||||
.and_then(LangItemTarget::as_struct)
|
||||
.map_or_else(
|
||||
|| self.err_ty(),
|
||||
|strukt| {
|
||||
TyKind::Adt(AdtId(strukt.into()), Substitution::empty(Interner))
|
||||
.intern(Interner)
|
||||
},
|
||||
),
|
||||
)
|
||||
.intern(Interner),
|
||||
Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(Interner),
|
||||
Literal::Int(_v, ty) => match ty {
|
||||
Some(int_ty) => {
|
||||
|
@ -428,9 +428,10 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
|
||||
// FIXME: ConstBlock/Path/Lit might actually evaluate to ref, but inference is unimplemented.
|
||||
Pat::Path(..) => true,
|
||||
Pat::ConstBlock(..) => true,
|
||||
Pat::Lit(expr) => {
|
||||
!matches!(body[*expr], Expr::Literal(Literal::String(..) | Literal::ByteString(..)))
|
||||
}
|
||||
Pat::Lit(expr) => !matches!(
|
||||
body[*expr],
|
||||
Expr::Literal(Literal::String(..) | Literal::CString(..) | Literal::ByteString(..))
|
||||
),
|
||||
Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false,
|
||||
}
|
||||
}
|
||||
|
@ -1133,15 +1133,26 @@ impl<'ctx> MirLowerCtx<'ctx> {
|
||||
let bytes = match l {
|
||||
hir_def::hir::Literal::String(b) => {
|
||||
let b = b.as_bytes();
|
||||
let mut data = vec![];
|
||||
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
|
||||
data.extend(0usize.to_le_bytes());
|
||||
data.extend(b.len().to_le_bytes());
|
||||
let mut mm = MemoryMap::default();
|
||||
mm.insert(0, b.to_vec());
|
||||
return Ok(Operand::from_concrete_const(data, mm, ty));
|
||||
}
|
||||
hir_def::hir::Literal::CString(b) => {
|
||||
let b = b.as_bytes();
|
||||
let bytes = b.iter().copied().chain(iter::once(0)).collect::<Vec<_>>();
|
||||
|
||||
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
|
||||
data.extend(0usize.to_le_bytes());
|
||||
data.extend(bytes.len().to_le_bytes());
|
||||
let mut mm = MemoryMap::default();
|
||||
mm.insert(0, bytes);
|
||||
return Ok(Operand::from_concrete_const(data, mm, ty));
|
||||
}
|
||||
hir_def::hir::Literal::ByteString(b) => {
|
||||
let mut data = vec![];
|
||||
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
|
||||
data.extend(0usize.to_le_bytes());
|
||||
data.extend(b.len().to_le_bytes());
|
||||
let mut mm = MemoryMap::default();
|
||||
|
@ -3572,3 +3572,18 @@ fn main() {
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cstring_literals() {
|
||||
check_types(
|
||||
r#"
|
||||
#[lang = "CStr"]
|
||||
pub struct CStr;
|
||||
|
||||
fn main() {
|
||||
c"ello";
|
||||
//^^^^^^^ &CStr
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ use crate::{utils::required_hashes, AssistContext, AssistId, AssistKind, Assists
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
// FIXME: This should support byte and c strings as well.
|
||||
let token = ctx.find_token_at_offset::<ast::String>()?;
|
||||
if token.is_raw() {
|
||||
return None;
|
||||
|
@ -39,7 +39,7 @@ fn try_extend_selection(
|
||||
) -> Option<TextRange> {
|
||||
let range = frange.range;
|
||||
|
||||
let string_kinds = [COMMENT, STRING, BYTE_STRING];
|
||||
let string_kinds = [COMMENT, STRING, BYTE_STRING, C_STRING];
|
||||
let list_kinds = [
|
||||
RECORD_PAT_FIELD_LIST,
|
||||
MATCH_ARM_LIST,
|
||||
|
@ -16,7 +16,10 @@ mod tests;
|
||||
use hir::{Name, Semantics};
|
||||
use ide_db::{FxHashMap, RootDatabase, SymbolKind};
|
||||
use syntax::{
|
||||
ast, AstNode, AstToken, NodeOrToken, SyntaxKind::*, SyntaxNode, TextRange, WalkEvent, T,
|
||||
ast::{self, IsString},
|
||||
AstNode, AstToken, NodeOrToken,
|
||||
SyntaxKind::*,
|
||||
SyntaxNode, TextRange, WalkEvent, T,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@ -440,7 +443,17 @@ fn traverse(
|
||||
&& ast::ByteString::can_cast(descended_token.kind())
|
||||
{
|
||||
if let Some(byte_string) = ast::ByteString::cast(token) {
|
||||
highlight_escape_string(hl, &byte_string, range.start());
|
||||
if !byte_string.is_raw() {
|
||||
highlight_escape_string(hl, &byte_string, range.start());
|
||||
}
|
||||
}
|
||||
} else if ast::CString::can_cast(token.kind())
|
||||
&& ast::CString::can_cast(descended_token.kind())
|
||||
{
|
||||
if let Some(c_string) = ast::CString::cast(token) {
|
||||
if !c_string.is_raw() {
|
||||
highlight_escape_string(hl, &c_string, range.start());
|
||||
}
|
||||
}
|
||||
} else if ast::Char::can_cast(token.kind())
|
||||
&& ast::Char::can_cast(descended_token.kind())
|
||||
|
@ -26,7 +26,7 @@ pub(super) fn token(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> O
|
||||
}
|
||||
|
||||
let highlight: Highlight = match token.kind() {
|
||||
STRING | BYTE_STRING => HlTag::StringLiteral.into(),
|
||||
STRING | BYTE_STRING | C_STRING => HlTag::StringLiteral.into(),
|
||||
INT_NUMBER if token.parent_ancestors().nth(1).map(|it| it.kind()) == Some(FIELD_EXPR) => {
|
||||
SymbolKind::Field.into()
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
use ide_db::base_db::{FileId, SourceDatabase};
|
||||
use ide_db::RootDatabase;
|
||||
use ide_db::{
|
||||
base_db::{FileId, SourceDatabase},
|
||||
RootDatabase,
|
||||
};
|
||||
use syntax::{
|
||||
AstNode, NodeOrToken, SourceFile, SyntaxKind::STRING, SyntaxToken, TextRange, TextSize,
|
||||
};
|
||||
|
@ -12,6 +12,8 @@ use super::*;
|
||||
// let _ = r"d";
|
||||
// let _ = b"e";
|
||||
// let _ = br"f";
|
||||
// let _ = c"g";
|
||||
// let _ = cr"h";
|
||||
// }
|
||||
pub(crate) const LITERAL_FIRST: TokenSet = TokenSet::new(&[
|
||||
T![true],
|
||||
@ -22,6 +24,7 @@ pub(crate) const LITERAL_FIRST: TokenSet = TokenSet::new(&[
|
||||
CHAR,
|
||||
STRING,
|
||||
BYTE_STRING,
|
||||
C_STRING,
|
||||
]);
|
||||
|
||||
pub(crate) fn literal(p: &mut Parser<'_>) -> Option<CompletedMarker> {
|
||||
|
@ -28,6 +28,7 @@ const GENERIC_ARG_FIRST: TokenSet = TokenSet::new(&[
|
||||
BYTE,
|
||||
STRING,
|
||||
BYTE_STRING,
|
||||
C_STRING,
|
||||
])
|
||||
.union(types::TYPE_FIRST);
|
||||
|
||||
|
@ -277,7 +277,7 @@ impl<'a> Converter<'a> {
|
||||
if !terminated {
|
||||
err = "Missing trailing `\"` symbol to terminate the string literal";
|
||||
}
|
||||
STRING
|
||||
C_STRING
|
||||
}
|
||||
rustc_lexer::LiteralKind::RawStr { n_hashes } => {
|
||||
if n_hashes.is_none() {
|
||||
@ -295,7 +295,7 @@ impl<'a> Converter<'a> {
|
||||
if n_hashes.is_none() {
|
||||
err = "Invalid raw string literal";
|
||||
}
|
||||
STRING
|
||||
C_STRING
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -131,6 +131,30 @@ SOURCE_FILE
|
||||
LITERAL
|
||||
BYTE_STRING "br\"f\""
|
||||
SEMICOLON ";"
|
||||
WHITESPACE "\n "
|
||||
LET_STMT
|
||||
LET_KW "let"
|
||||
WHITESPACE " "
|
||||
WILDCARD_PAT
|
||||
UNDERSCORE "_"
|
||||
WHITESPACE " "
|
||||
EQ "="
|
||||
WHITESPACE " "
|
||||
LITERAL
|
||||
C_STRING "c\"g\""
|
||||
SEMICOLON ";"
|
||||
WHITESPACE "\n "
|
||||
LET_STMT
|
||||
LET_KW "let"
|
||||
WHITESPACE " "
|
||||
WILDCARD_PAT
|
||||
UNDERSCORE "_"
|
||||
WHITESPACE " "
|
||||
EQ "="
|
||||
WHITESPACE " "
|
||||
LITERAL
|
||||
C_STRING "cr\"h\""
|
||||
SEMICOLON ";"
|
||||
WHITESPACE "\n"
|
||||
R_CURLY "}"
|
||||
WHITESPACE "\n"
|
||||
|
@ -9,4 +9,6 @@ fn foo() {
|
||||
let _ = r"d";
|
||||
let _ = b"e";
|
||||
let _ = br"f";
|
||||
let _ = c"g";
|
||||
let _ = cr"h";
|
||||
}
|
||||
|
@ -288,6 +288,7 @@ impl ast::ArrayExpr {
|
||||
pub enum LiteralKind {
|
||||
String(ast::String),
|
||||
ByteString(ast::ByteString),
|
||||
CString(ast::CString),
|
||||
IntNumber(ast::IntNumber),
|
||||
FloatNumber(ast::FloatNumber),
|
||||
Char(ast::Char),
|
||||
@ -319,6 +320,9 @@ impl ast::Literal {
|
||||
if let Some(t) = ast::ByteString::cast(token.clone()) {
|
||||
return LiteralKind::ByteString(t);
|
||||
}
|
||||
if let Some(t) = ast::CString::cast(token.clone()) {
|
||||
return LiteralKind::CString(t);
|
||||
}
|
||||
if let Some(t) = ast::Char::cast(token.clone()) {
|
||||
return LiteralKind::Char(t);
|
||||
}
|
||||
|
@ -90,6 +90,27 @@ impl AstToken for ByteString {
|
||||
fn syntax(&self) -> &SyntaxToken { &self.syntax }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct CString {
|
||||
pub(crate) syntax: SyntaxToken,
|
||||
}
|
||||
impl std::fmt::Display for CString {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(&self.syntax, f)
|
||||
}
|
||||
}
|
||||
impl AstToken for CString {
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == C_STRING }
|
||||
fn cast(syntax: SyntaxToken) -> Option<Self> {
|
||||
if Self::can_cast(syntax.kind()) {
|
||||
Some(Self { syntax })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxToken { &self.syntax }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct IntNumber {
|
||||
pub(crate) syntax: SyntaxToken,
|
||||
|
@ -145,6 +145,10 @@ impl QuoteOffsets {
|
||||
}
|
||||
|
||||
pub trait IsString: AstToken {
|
||||
const RAW_PREFIX: &'static str;
|
||||
fn is_raw(&self) -> bool {
|
||||
self.text().starts_with(Self::RAW_PREFIX)
|
||||
}
|
||||
fn quote_offsets(&self) -> Option<QuoteOffsets> {
|
||||
let text = self.text();
|
||||
let offsets = QuoteOffsets::new(text)?;
|
||||
@ -183,20 +187,18 @@ pub trait IsString: AstToken {
|
||||
cb(text_range + offset, unescaped_char);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl IsString for ast::String {}
|
||||
|
||||
impl ast::String {
|
||||
pub fn is_raw(&self) -> bool {
|
||||
self.text().starts_with('r')
|
||||
}
|
||||
pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
|
||||
fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
|
||||
let contents_range = self.text_range_between_quotes()?;
|
||||
assert!(TextRange::up_to(contents_range.len()).contains_range(range));
|
||||
Some(range + contents_range.start())
|
||||
}
|
||||
}
|
||||
|
||||
impl IsString for ast::String {
|
||||
const RAW_PREFIX: &'static str = "r";
|
||||
}
|
||||
|
||||
impl ast::String {
|
||||
pub fn value(&self) -> Option<Cow<'_, str>> {
|
||||
if self.is_raw() {
|
||||
let text = self.text();
|
||||
@ -235,13 +237,11 @@ impl ast::String {
|
||||
}
|
||||
}
|
||||
|
||||
impl IsString for ast::ByteString {}
|
||||
impl IsString for ast::ByteString {
|
||||
const RAW_PREFIX: &'static str = "br";
|
||||
}
|
||||
|
||||
impl ast::ByteString {
|
||||
pub fn is_raw(&self) -> bool {
|
||||
self.text().starts_with("br")
|
||||
}
|
||||
|
||||
pub fn value(&self) -> Option<Cow<'_, [u8]>> {
|
||||
if self.is_raw() {
|
||||
let text = self.text();
|
||||
@ -280,6 +280,49 @@ impl ast::ByteString {
|
||||
}
|
||||
}
|
||||
|
||||
impl IsString for ast::CString {
|
||||
const RAW_PREFIX: &'static str = "cr";
|
||||
}
|
||||
|
||||
impl ast::CString {
|
||||
pub fn value(&self) -> Option<Cow<'_, str>> {
|
||||
if self.is_raw() {
|
||||
let text = self.text();
|
||||
let text =
|
||||
&text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
|
||||
return Some(Cow::Borrowed(text));
|
||||
}
|
||||
|
||||
let text = self.text();
|
||||
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
|
||||
|
||||
let mut buf = String::new();
|
||||
let mut prev_end = 0;
|
||||
let mut has_error = false;
|
||||
unescape_literal(text, Mode::Str, &mut |char_range, unescaped_char| match (
|
||||
unescaped_char,
|
||||
buf.capacity() == 0,
|
||||
) {
|
||||
(Ok(c), false) => buf.push(c),
|
||||
(Ok(_), true) if char_range.len() == 1 && char_range.start == prev_end => {
|
||||
prev_end = char_range.end
|
||||
}
|
||||
(Ok(c), true) => {
|
||||
buf.reserve_exact(text.len());
|
||||
buf.push_str(&text[..prev_end]);
|
||||
buf.push(c);
|
||||
}
|
||||
(Err(_), _) => has_error = true,
|
||||
});
|
||||
|
||||
match (has_error, buf.capacity() == 0) {
|
||||
(true, _) => None,
|
||||
(false, true) => Some(Cow::Borrowed(text)),
|
||||
(false, false) => Some(Cow::Owned(buf)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::IntNumber {
|
||||
pub fn radix(&self) -> Radix {
|
||||
match self.text().get(..2).unwrap_or_default() {
|
||||
|
@ -39,7 +39,7 @@ fn reparse_token(
|
||||
let prev_token = root.covering_element(edit.delete).as_token()?.clone();
|
||||
let prev_token_kind = prev_token.kind();
|
||||
match prev_token_kind {
|
||||
WHITESPACE | COMMENT | IDENT | STRING => {
|
||||
WHITESPACE | COMMENT | IDENT | STRING | BYTE_STRING | C_STRING => {
|
||||
if prev_token_kind == WHITESPACE || prev_token_kind == COMMENT {
|
||||
// removing a new line may extends previous token
|
||||
let deleted_range = edit.delete - prev_token.text_range().start();
|
||||
|
@ -573,10 +573,11 @@ impl Field {
|
||||
|
||||
fn lower(grammar: &Grammar) -> AstSrc {
|
||||
let mut res = AstSrc {
|
||||
tokens: "Whitespace Comment String ByteString IntNumber FloatNumber Char Byte Ident"
|
||||
.split_ascii_whitespace()
|
||||
.map(|it| it.to_string())
|
||||
.collect::<Vec<_>>(),
|
||||
tokens:
|
||||
"Whitespace Comment String ByteString CString IntNumber FloatNumber Char Byte Ident"
|
||||
.split_ascii_whitespace()
|
||||
.map(|it| it.to_string())
|
||||
.collect::<Vec<_>>(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
@ -9,7 +9,7 @@ use rustc_lexer::unescape::{self, unescape_literal, Mode};
|
||||
|
||||
use crate::{
|
||||
algo,
|
||||
ast::{self, HasAttrs, HasVisibility},
|
||||
ast::{self, HasAttrs, HasVisibility, IsString},
|
||||
match_ast, AstNode, SyntaxError,
|
||||
SyntaxKind::{CONST, FN, INT_NUMBER, TYPE_ALIAS},
|
||||
SyntaxNode, SyntaxToken, TextSize, T,
|
||||
@ -156,6 +156,17 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::LiteralKind::CString(s) => {
|
||||
if !s.is_raw() {
|
||||
if let Some(without_quotes) = unquote(text, 2, '"') {
|
||||
unescape_literal(without_quotes, Mode::ByteStr, &mut |range, char| {
|
||||
if let Err(err) = char {
|
||||
push_err(1, range.start, err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::LiteralKind::Char(_) => {
|
||||
if let Some(without_quotes) = unquote(text, 1, '\'') {
|
||||
unescape_literal(without_quotes, Mode::Char, &mut |range, char| {
|
||||
|
Loading…
Reference in New Issue
Block a user