Skip to content

EmitterWriter::get_max_line_num works incorrectly #64447

Closed
@AnthonyMikh

Description

@AnthonyMikh

TL;DR: EmitterWriter::get_max_line_num unconditionally returns self.get_multispan_max_line_num(span) no matter what is contained in children.

Longer explanation: let's take a look at the code of method (it's rather short):

    fn get_max_line_num(&mut self, span: &MultiSpan, children: &[SubDiagnostic]) -> usize {
        let mut max = 0;

        let primary = self.get_multispan_max_line_num(span);
        max = if primary > max { primary } else { max };

        for sub in children {
            let sub_result = self.get_multispan_max_line_num(&sub.span);
            max = if sub_result > max { primary } else { max };
        }
        max
    }

Here self.get_multispan_max_line_num(span) returns a plain usize. Firstly, since 0 is the smallest possible value of usize, the first three lines can be rewritten without changing the meaning as

let primary = self.get_multispan_max_line_num(span);
let mut max = primary;

So after executing these lines max == primary. Secondly, in the loop if compares sub_result with max but assigns either max or primary. If max == primary in the beginning of iteration, then this also holds in the end of iteration. Since this preposition holds before executing the loop, it also holds after executing the loop, so the whole method just returns primary, QED.

Erroneous code was introduced in 71ec286.

Unfortunately, I'm not familliar enough with the code to propose the correct fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions