zesterer / ariadne

A fancy diagnostics & error reporting crate
https://crates.io/crates/ariadne
MIT License
1.61k stars 72 forks source link

Label fails to render when using emojis as source #62

Closed GrandChaman closed 1 year ago

GrandChaman commented 1 year ago

I noticed that when using an emoji in the source, depending on the range I specify to associate a label to it, I get a label that make sense or not.

Code to reproduce:

use ariadne::{Label, Report, ReportKind, Source};

fn main() {
    for i in 1..=4 {
        println!("Using range {:?} for a 4-bytes emoji", 0..i);
        let mut report = Report::build(ReportKind::Error, (), 0);
        report = report.with_label(Label::new(0..i).with_message("That is very haha ๐Ÿ˜‚"));
        report.finish().print(Source::from("๐Ÿ˜‚")).unwrap();
    }
}

The output:

Using range 0..1 for a 4-bytes emoji
Error:
   โ•ญโ”€[<unknown>:1:1]
   โ”‚
 1 โ”‚ ๐Ÿ˜‚
   ยท โ”ฌ
   ยท โ•ฐโ”€โ”€ That is very haha ๐Ÿ˜‚
โ”€โ”€โ”€โ•ฏ
Using range 0..2 for a 4-bytes emoji
Error:
   โ•ญโ”€[<unknown>:1:1]
   โ”‚
 1 โ”‚ ๐Ÿ˜‚
   ยท โ”€โ”ฌ
   ยท  โ•ฐโ”€โ”€ That is very haha ๐Ÿ˜‚
โ”€โ”€โ”€โ•ฏ
Using range 0..3 for a 4-bytes emoji
Error:
   โ•ญโ”€[<unknown>:1:1]
   โ”‚
   ยท
โ”€โ”€โ”€โ•ฏ
Using range 0..4 for a 4-bytes emoji
Error:
   โ•ญโ”€[<unknown>:1:1]
   โ”‚
 1 โ”‚ โ•ญโ”€โ–ถ ๐Ÿ˜‚
โ”€โ”€โ”€โ•ฏ

I think the versions with the ranges 0..1, 0..2 and 0..3 don't make sense, but I would expect the last range (0..4) to display the label correctly.

zesterer commented 1 year ago

Ariadne currently uses character indices, not byte indices. I consider this to be a mistake that I want to fix eventually. I'm going to close this in favour of #57, but I'm happy to reopen it if you believe there's more going on here.